diff --git a/app/controllers/impressionist_controller.rb b/app/controllers/impressionist_controller.rb index b00cce0..d9a9119 100644 --- a/app/controllers/impressionist_controller.rb +++ b/app/controllers/impressionist_controller.rb @@ -77,7 +77,7 @@ module ImpressionistController end def unique_instance?(impressionable, unique_opts) - return unique_opts.blank? || !impressionable.impressions.where(unique_query(unique_opts)).exists? + return unique_opts.blank? || !impressionable.impressions.where(unique_query(unique_opts, impressionable)).exists? end def unique?(unique_opts) @@ -85,8 +85,8 @@ module ImpressionistController end # creates the query to check for uniqueness - def unique_query(unique_opts) - full_statement = direct_create_statement + def unique_query(unique_opts,impressionable=nil) + full_statement = direct_create_statement({},impressionable) # reduce the full statement to the params we need for the specified unique options unique_opts.reduce({}) do |query, param| query[param] = full_statement[param] @@ -95,10 +95,10 @@ module ImpressionistController end # creates a statment hash that contains default values for creating an impression. - def direct_create_statement(query_params={}) + def direct_create_statement(query_params={},impressionable=nil) query_params.reverse_merge!( :impressionable_type => controller_name.singularize.camelize, - :impressionable_id=> params[:id] + :impressionable_id => impressionable.present? ? impressionable.id : params[:id] ) associative_create_statement(query_params) end diff --git a/tests/test_app/.gitignore b/tests/test_app/.gitignore index 351f638..0bfe114 100644 --- a/tests/test_app/.gitignore +++ b/tests/test_app/.gitignore @@ -15,3 +15,4 @@ /coverage /log/*.log /tmp +/.idea diff --git a/tests/test_app/Gemfile b/tests/test_app/Gemfile index e7df84c..71c5bdd 100644 --- a/tests/test_app/Gemfile +++ b/tests/test_app/Gemfile @@ -34,7 +34,7 @@ end group :development, :test do gem 'autotest-notification' - gem 'rspec-rails' + gem 'rspec-rails', '~> 2.14.0' gem 'spork' end @@ -45,3 +45,4 @@ group :test do end gem 'jquery-rails' +gem 'friendly_id', '~> 4.0.10.1' diff --git a/tests/test_app/app/controllers/profiles_controller.rb b/tests/test_app/app/controllers/profiles_controller.rb new file mode 100644 index 0000000..887186f --- /dev/null +++ b/tests/test_app/app/controllers/profiles_controller.rb @@ -0,0 +1,16 @@ +class ProfilesController < ApplicationController + helper_method :current_user + + def show + @profile = Profile.friendly.find params[:id] + impressionist(@profile, nil, :unique => [:impressionable_type, :impressionable_id]) + end + + def current_user + if session[:user_id] + user = User.new + user.id = session[:user_id] + @current_user ||= user + end + end +end diff --git a/tests/test_app/app/models/profile.rb b/tests/test_app/app/models/profile.rb new file mode 100644 index 0000000..9ab2353 --- /dev/null +++ b/tests/test_app/app/models/profile.rb @@ -0,0 +1,6 @@ +class Profile < ActiveRecord::Base + extend FriendlyId + + friendly_id :username, use: :slugged + is_impressionable +end diff --git a/tests/test_app/app/views/profiles/show.html.erb b/tests/test_app/app/views/profiles/show.html.erb new file mode 100644 index 0000000..6b48900 --- /dev/null +++ b/tests/test_app/app/views/profiles/show.html.erb @@ -0,0 +1,3 @@ +

User Public Profile

+ +

User Name: <%= @profile.username %>

diff --git a/tests/test_app/config/routes.rb b/tests/test_app/config/routes.rb index 76cb5b5..a9557a9 100644 --- a/tests/test_app/config/routes.rb +++ b/tests/test_app/config/routes.rb @@ -1,3 +1,4 @@ TestApp::Application.routes.draw do resources :articles, :posts, :widgets, :dummy + get 'profiles/[:id]' => 'profiles#show' end diff --git a/tests/test_app/db/migrate/20150207135825_create_profiles.rb b/tests/test_app/db/migrate/20150207135825_create_profiles.rb new file mode 100644 index 0000000..008c8fb --- /dev/null +++ b/tests/test_app/db/migrate/20150207135825_create_profiles.rb @@ -0,0 +1,10 @@ +class CreateProfiles < ActiveRecord::Migration + def change + create_table :profiles do |t| + t.string :username + t.string :slug + + t.timestamps + end + end +end diff --git a/tests/test_app/db/migrate/20150207140310_create_friendly_id_slugs.rb b/tests/test_app/db/migrate/20150207140310_create_friendly_id_slugs.rb new file mode 100644 index 0000000..bb80e48 --- /dev/null +++ b/tests/test_app/db/migrate/20150207140310_create_friendly_id_slugs.rb @@ -0,0 +1,18 @@ +class CreateFriendlyIdSlugs < ActiveRecord::Migration + + def self.up + create_table :friendly_id_slugs do |t| + t.string :slug, :null => false + t.integer :sluggable_id, :null => false + t.string :sluggable_type, :limit => 40 + t.datetime :created_at + end + add_index :friendly_id_slugs, :sluggable_id + add_index :friendly_id_slugs, [:slug, :sluggable_type], :unique => true + add_index :friendly_id_slugs, :sluggable_type + end + + def self.down + drop_table :friendly_id_slugs + end +end diff --git a/tests/test_app/db/schema.rb b/tests/test_app/db/schema.rb index 9508e50..fd9df3f 100644 --- a/tests/test_app/db/schema.rb +++ b/tests/test_app/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20130719024021) do +ActiveRecord::Schema.define(:version => 20150207140310) do create_table "articles", :force => true do |t| t.string "name" @@ -19,6 +19,17 @@ ActiveRecord::Schema.define(:version => 20130719024021) do t.datetime "updated_at", :null => false end + create_table "friendly_id_slugs", :force => true do |t| + t.string "slug", :null => false + t.integer "sluggable_id", :null => false + t.string "sluggable_type", :limit => 40 + t.datetime "created_at" + end + + add_index "friendly_id_slugs", ["slug", "sluggable_type"], :name => "index_friendly_id_slugs_on_slug_and_sluggable_type", :unique => true + add_index "friendly_id_slugs", ["sluggable_id"], :name => "index_friendly_id_slugs_on_sluggable_id" + add_index "friendly_id_slugs", ["sluggable_type"], :name => "index_friendly_id_slugs_on_sluggable_type" + create_table "impressions", :force => true do |t| t.string "impressionable_type" t.integer "impressionable_id" @@ -50,6 +61,13 @@ ActiveRecord::Schema.define(:version => 20130719024021) do t.datetime "updated_at", :null => false end + create_table "profiles", :force => true do |t| + t.string "username" + t.string "slug" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + create_table "widgets", :force => true do |t| t.string "name" t.integer "impressions_count", :default => 0 diff --git a/tests/test_app/spec/controllers/impressionist_uniqueness_spec.rb b/tests/test_app/spec/controllers/impressionist_uniqueness_spec.rb index 0986c57..dd921cc 100644 --- a/tests/test_app/spec/controllers/impressionist_uniqueness_spec.rb +++ b/tests/test_app/spec/controllers/impressionist_uniqueness_spec.rb @@ -306,6 +306,22 @@ describe DummyController do end + describe 'impressionist with friendly id' do + it 'should unique' do + impressionable = Profile.find 1 + # get 'profiles/test_profile' + # get 'profiles/test_profile' + controller.stub(:controller_name).and_return('profile') + controller.stub(:action_name).and_return('show') + controller.stub(:params).and_return({id: 'test_profile'}) + controller.request.stub(:remote_ip).and_return('1.2.3.4') + + controller.impressionist(impressionable, nil, :unique => [:impressionable_type, :impressionable_id]) + controller.impressionist(impressionable, nil, :unique => [:impressionable_type, :impressionable_id]) + Impression.should have(@impression_count + 1).records + end + end + shared_examples_for 'an impressionable action' do it 'should record an impression' do controller.impressionist_subapp_filter(condition) diff --git a/tests/test_app/spec/fixtures/profiles.yml b/tests/test_app/spec/fixtures/profiles.yml new file mode 100644 index 0000000..c6d5f01 --- /dev/null +++ b/tests/test_app/spec/fixtures/profiles.yml @@ -0,0 +1,4 @@ +one: + id: 1 + username: test_profile + slug: test_profile diff --git a/tests/test_app/spec/models/model_spec.rb b/tests/test_app/spec/models/model_spec.rb index 40a9e7f..2ed4156 100644 --- a/tests/test_app/spec/models/model_spec.rb +++ b/tests/test_app/spec/models/model_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe Impression do - fixtures :articles,:impressions,:posts + fixtures :articles,:impressions,:posts,:profiles before(:each) do @article = Article.find(1) diff --git a/tests/test_app/spec/rails_generators/rails_generators_spec.rb b/tests/test_app/spec/rails_generators/rails_generators_spec.rb index 79022e8..076abdf 100644 --- a/tests/test_app/spec/rails_generators/rails_generators_spec.rb +++ b/tests/test_app/spec/rails_generators/rails_generators_spec.rb @@ -4,7 +4,7 @@ require 'systemu' # FIXME this test might break the others if run before them # started fixing @nbit001 describe Impressionist, :migration do - fixtures :articles,:impressions,:posts + fixtures :articles,:impressions,:posts,:profiles it "should delete existing migration and generate the migration file" do pending migrations_dir = "#{Rails.root}/db/migrate"