Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #182 from tauhidul35/master

Unique impressionist of a instance which is using friendly_id
  • Loading branch information...
commit 432b77e40d9370d5951ba0bf5084e81a74c647b7 2 parents cacdd66 + 66c7533
@johnmcaliley johnmcaliley authored
View
10 app/controllers/impressionist_controller.rb
@@ -77,7 +77,7 @@ def conditional?(condition)
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 @@ def unique?(unique_opts)
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 @@ def unique_query(unique_opts)
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
View
3  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 => '../'
View
3  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 => '../'
View
1  tests/test_app/.gitignore
@@ -15,3 +15,4 @@
/coverage
/log/*.log
/tmp
+/.idea
View
3  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.9'
View
14 tests/test_app/app/controllers/profiles_controller.rb
@@ -0,0 +1,14 @@
+class ProfilesController < ApplicationController
+ helper_method :current_user
+
+ def show
+ end
+
+ def current_user
+ if session[:user_id]
+ user = User.new
+ user.id = session[:user_id]
+ @current_user ||= user
+ end
+ end
+end
View
6 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
View
3  tests/test_app/app/views/profiles/show.html.erb
@@ -0,0 +1,3 @@
+<h2>User Public Profile</h2>
+
+<p>User Name: <%= @profile.username %></p>
View
1  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
View
10 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
View
18 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
View
20 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 @@
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 @@
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
View
15 tests/test_app/spec/controllers/impressionist_uniqueness_spec.rb
@@ -306,6 +306,21 @@
end
+ describe 'impressionist with friendly id' do
+ it 'should unique' do
+ 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: impressionable.slug})
+ 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)
View
4 tests/test_app/spec/fixtures/profiles.yml
@@ -0,0 +1,4 @@
+one:
+ id: 1
+ username: test_profile
+ slug: test_profile
View
2  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)
View
2  tests/test_app/spec/rails_generators/rails_generators_spec.rb
@@ -4,7 +4,7 @@
# 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"
Please sign in to comment.
Something went wrong with that request. Please try again.