From f77d187f667bf0cb355bd2e9ed1346ac3cffa58a Mon Sep 17 00:00:00 2001 From: "Md. Tauhidul Islam" Date: Sat, 7 Feb 2015 22:08:32 +0600 Subject: [PATCH 1/4] Unique impressionist count for objects which are using friendly_id --- app/controllers/impressionist_controller.rb | 10 +++++----- tests/test_app/.gitignore | 1 + tests/test_app/Gemfile | 3 ++- .../app/controllers/profiles_controller.rb | 16 +++++++++++++++ tests/test_app/app/models/profile.rb | 6 ++++++ .../test_app/app/views/profiles/show.html.erb | 3 +++ tests/test_app/config/routes.rb | 1 + .../migrate/20150207135825_create_profiles.rb | 10 ++++++++++ ...20150207140310_create_friendly_id_slugs.rb | 18 +++++++++++++++++ tests/test_app/db/schema.rb | 20 ++++++++++++++++++- .../impressionist_uniqueness_spec.rb | 16 +++++++++++++++ tests/test_app/spec/fixtures/profiles.yml | 4 ++++ tests/test_app/spec/models/model_spec.rb | 2 +- .../rails_generators/rails_generators_spec.rb | 2 +- 14 files changed, 103 insertions(+), 9 deletions(-) create mode 100644 tests/test_app/app/controllers/profiles_controller.rb create mode 100644 tests/test_app/app/models/profile.rb create mode 100644 tests/test_app/app/views/profiles/show.html.erb create mode 100644 tests/test_app/db/migrate/20150207135825_create_profiles.rb create mode 100644 tests/test_app/db/migrate/20150207140310_create_friendly_id_slugs.rb create mode 100644 tests/test_app/spec/fixtures/profiles.yml 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" From 8573b40e6fd29907e5d48d5a068b0708b4366b21 Mon Sep 17 00:00:00 2001 From: "Md. Tauhidul Islam" Date: Sun, 8 Feb 2015 00:43:38 +0600 Subject: [PATCH 2/4] Add friendly_id to test group in gem file --- Gemfile | 1 + tests/test_app/app/controllers/profiles_controller.rb | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index 37823f5..09ab026 100644 --- a/Gemfile +++ b/Gemfile @@ -21,6 +21,7 @@ group :test do gem 'rspec-rails' gem 'simplecov' gem 'systemu' + gem 'friendly_id', '~> 4.0.10.1' end gemspec diff --git a/tests/test_app/app/controllers/profiles_controller.rb b/tests/test_app/app/controllers/profiles_controller.rb index 887186f..a8adf89 100644 --- a/tests/test_app/app/controllers/profiles_controller.rb +++ b/tests/test_app/app/controllers/profiles_controller.rb @@ -2,8 +2,6 @@ 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 From 5573aebeaad385ae0ee106406a25a12fa5c8045d Mon Sep 17 00:00:00 2001 From: "Md. Tauhidul Islam" Date: Sun, 8 Feb 2015 00:54:41 +0600 Subject: [PATCH 3/4] Change friendly_id gem version --- Gemfile | 2 +- tests/test_app/Gemfile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index 09ab026..71b0f6b 100644 --- a/Gemfile +++ b/Gemfile @@ -21,7 +21,7 @@ group :test do gem 'rspec-rails' gem 'simplecov' gem 'systemu' - gem 'friendly_id', '~> 4.0.10.1' + gem 'friendly_id', '~> 4.0.9' end gemspec diff --git a/tests/test_app/Gemfile b/tests/test_app/Gemfile index 71c5bdd..0446278 100644 --- a/tests/test_app/Gemfile +++ b/tests/test_app/Gemfile @@ -45,4 +45,4 @@ group :test do end gem 'jquery-rails' -gem 'friendly_id', '~> 4.0.10.1' +gem 'friendly_id', '~> 4.0.9' From 66c7533f47aa3dea250a5ec09822fe85a4c046c7 Mon Sep 17 00:00:00 2001 From: "Md. Tauhidul Islam" Date: Sun, 8 Feb 2015 02:03:34 +0600 Subject: [PATCH 4/4] - Fix test code for unique impressionist of instance with friendly_id - Fixed rspec-rails version for run previous test cases - Add friendly_id in gemfiles for run test case --- Gemfile | 1 - gemfiles/rails32.gemfile | 3 ++- gemfiles/rails40.gemfile | 3 ++- .../spec/controllers/impressionist_uniqueness_spec.rb | 7 +++---- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Gemfile b/Gemfile index 71b0f6b..37823f5 100644 --- a/Gemfile +++ b/Gemfile @@ -21,7 +21,6 @@ group :test do gem 'rspec-rails' gem 'simplecov' gem 'systemu' - gem 'friendly_id', '~> 4.0.9' end gemspec diff --git a/gemfiles/rails32.gemfile b/gemfiles/rails32.gemfile index 8edb965..08a2fae 100644 --- a/gemfiles/rails32.gemfile +++ b/gemfiles/rails32.gemfile @@ -18,9 +18,10 @@ group :test do gem 'minitest' gem 'minitest-rails' gem 'rails', '~> 3.2.15' - gem 'rspec-rails' + gem 'rspec-rails', '~> 2.14.0' gem 'simplecov' gem 'systemu' + gem 'friendly_id', '~> 4.0.9' end gemspec :path => '../' diff --git a/gemfiles/rails40.gemfile b/gemfiles/rails40.gemfile index 60d8569..beb41c9 100644 --- a/gemfiles/rails40.gemfile +++ b/gemfiles/rails40.gemfile @@ -18,9 +18,10 @@ group :test do gem 'minitest' gem 'minitest-rails' gem 'rails', '~> 4.0.1' - gem 'rspec-rails' + gem 'rspec-rails', '~> 2.14.0' gem 'simplecov' gem 'systemu' + gem 'friendly_id', '~> 5.1.0' end gemspec :path => '../' diff --git a/tests/test_app/spec/controllers/impressionist_uniqueness_spec.rb b/tests/test_app/spec/controllers/impressionist_uniqueness_spec.rb index dd921cc..8fed026 100644 --- a/tests/test_app/spec/controllers/impressionist_uniqueness_spec.rb +++ b/tests/test_app/spec/controllers/impressionist_uniqueness_spec.rb @@ -308,12 +308,11 @@ describe DummyController do describe 'impressionist with friendly id' do it 'should unique' do - impressionable = Profile.find 1 - # get 'profiles/test_profile' - # get 'profiles/test_profile' + impressionable = Profile.create({username: 'test_profile', slug: '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.stub(:params).and_return({id: impressionable.slug}) controller.request.stub(:remote_ip).and_return('1.2.3.4') controller.impressionist(impressionable, nil, :unique => [:impressionable_type, :impressionable_id])