EzDevInfo.com

Impressionist

Visual tool to create impress.js presentations

prevent deleting all impressions records if item soft deleted with paranoia gem

I use paranoia gem for soft deleting items, which are also impressionable (impressionist gem). Model config is very simple:

is_impressionable
acts_as_paranoid

When I soft delete an item, all impression count records are destroyed.

Is there a way to prevent such behaviour that impression records are kept at all times?

thanks, R


Source: (StackOverflow)

Link to Order Index Rails 4

I am using Rails 4 with the Impressionist gem to get view count of my Articles.

On my index page I have a link labeled "Most Popular"

I also have access to a method that will order articles by the view count:

@articles = Article.order('impressions_count ASC')

What is the best way to order the index by impression_count when a user clicks the "most popular button?" I am having trouble finding documentation on this.

Here is my articles_controller.rb

class ArticlesController < ApplicationController
  load_and_authorize_resource
  before_action :set_article, only: [:show, :edit, :update, :destroy]

  def index
    if params[:q].present?
       @articles = Article.search(params[:q], misspellings: {edit_distance: 1}, page: params[:page], per_page: 12)
       @search = current_or_guest_user.searches.create!(query: params[:q], searched_at: Time.now)
    else
       @articles = Article.order('impressions_count ASC').page(params[:page]).per(12)
       @search = current_or_guest_user.searches.create!(query: params[:q], searched_at: Time.now)
    end
    if @articles.blank?
       return redirect_to request_path
       @search = current_or_guest_user.searches.create!(query: params[:q], searched_at: Time.now)
    end
    get_query
  end

  def autocomplete
    @articles = Article.search params[:term], autocomplete: true
    render json: @articles
  end

  def search
    @articles = Article.search params[:q], suggest: true, page: params[:page], per_page: 5
    @search = current_or_guest_user.searches.create!(query: params[:q], searched_at: Time.now)
    render 'index'
  end

  def show
    impressionist(@article, nil, { unique: [:session_hash] })
    @skip_error = true
    @subarticles = @article.subarticles.approved.order(:cached_votes_score => :desc)
   if request.path != article_path(@article)
      redirect_to @article, status: :moved_permanently
    else
        respond_to do |format|
        format.html # show.html.erb
        format.json { render json: @article }
      end
    end
  end

  def new
  end

  def edit
  end

  def create
     respond_to do |format|
      if @article.save
        format.html { redirect_to @article, notice: 'Article was successfully created.' }
        format.json { render :show, status: :created, location: @article }
      else
        format.html { render :new }
        format.json { render json: @article.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @article.update(article_params)
        format.html { redirect_to @article, notice: 'Article was successfully updated.' }
        format.json { render :show, status: :ok, location: @article }
      else
        format.html { render :edit }
        format.json { render json: @article.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @article.destroy
    respond_to do |format|
      format.html { redirect_to articles_url, notice: 'Article was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_article
      @article = Article.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def article_params
      params.require(:article).permit(:title, :specific, :category, :aka, :image1, :image2, :video1, :video2)
    end

    def get_query
      @userquery = params[:q]
    end
end

Source: (StackOverflow)

Advertisements

Rails:Impressionist - impression_count not incrementing

I am having some difficulty interpreting the results I am seeing from my impressionist gem set up. I am trying to stratify views each day over the last 30 days. Right of the bat things are not working.

I am working under the assumption that

    current_user.impression_count 

returns all views - ALL of them, unique or not as it increments as I refresh the @user show page

What I don't get it this....

   @profile_views_one_day = current_user.impressionist_count(:start_date=> 1.day.ago.strftime('%Y-%m-%d'), :end_date=> Time.now.strftime('%Y-%m-%d'))

This does not increment at all. Therefore I assumed this is based on session uniqueness or request uniques etc. But if I log out and then in, no increment. If I upload my development site to my production server and log in from diffent IP addresses, there is no increment.

Have I got the code wrong? The code is meant to give the views for the last 24 hours.


Source: (StackOverflow)

How to use impressionist gem with hashed ID?

I use impressionist gem for statistics in my app. When I did use URL like this:

/music/52(by ID)

it did work. But when I changed ID to hashed ID it cannot find my statistics. When I changed the type of impressionable_id from int4 to varchar it prints the next error:

`PG::UndefinedFunction: ERROR: operator does not exist: character varying = integer at character 77 HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. : SELECT 1 AS one FROM "impressions" WHERE "impressions"."impressionable_id" = $1 AND "impressions"."session_hash" = $2 LIMIT 1`

How can I fix it and start to find statistics with hashed ID?


Source: (StackOverflow)

Super simple setup with impressionist, mismatch between impressionist_count and impressions_count

Using Rails 4 and Impressionist 1.5.1


I have a ProductsController and a Product model.

In my show action:

def show
  impressionist(@product, "unique view", :unique => [:session_hash])
end

In my model:

class Product < ActiveRecord::Base
  is_impressionable counter_cache: true
end 

And my Product schema:

create_table "products", force: true do |t|
  t.string   "name",                                        null: false
  t.integer  "impressions_count",           default: 0
end

And there's a mismatch between the counts:

irb(main):001:0> Product.find_by(slug: 'test').impressions_count
=> 57
irb(main):002:0> Product.find_by(slug: 'test').impressionist_count
=> 70

Source: (StackOverflow)

Using Impressionist in Rails 4.1 is not logging views by session hash

Pretty simple stuff:

class ProductsController < ApplicationController
  impressionist actions: [:show], unique: [:session_hash]
end

In the model:

class Product < ActiveRecord::Base
  is_impressionable counter_cache: true
end

The views aren't being logged properly, and I'm not sure why. This exact same code used to work with earlier version of Rails.

One suggestion is: https://github.com/charlotte-ruby/impressionist/issues/177

...and just like that I found a solution. It's due to lazy loading of the session. Putting a session[:init] = true before accessing the session works. It now looks like this:

ImpressionistController::InstanceMethods.class_eval do    
  def session_hash
    session[:init] = true
    request.session.id   
  end 
end 

Hope that helps!


How can I apply this fix? I tried to create this impressionist_controller.rb file in my app/controllers folder and got:

gemset/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:478:in `load_missing_constant': Circular dependency detected while autoloading constant ImpressionistController (RuntimeError)


Source: (StackOverflow)