Merge pull request #182 from tauhidul35/master

Unique impressionist of a instance which is using friendly_id
This commit is contained in:
John McAliley 2015-02-09 20:21:13 -05:00
commit 432b77e40d
16 changed files with 104 additions and 11 deletions

View File

@ -77,7 +77,7 @@ module ImpressionistController
end end
def unique_instance?(impressionable, unique_opts) 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 end
def unique?(unique_opts) def unique?(unique_opts)
@ -85,8 +85,8 @@ module ImpressionistController
end end
# creates the query to check for uniqueness # creates the query to check for uniqueness
def unique_query(unique_opts) def unique_query(unique_opts,impressionable=nil)
full_statement = direct_create_statement full_statement = direct_create_statement({},impressionable)
# reduce the full statement to the params we need for the specified unique options # reduce the full statement to the params we need for the specified unique options
unique_opts.reduce({}) do |query, param| unique_opts.reduce({}) do |query, param|
query[param] = full_statement[param] query[param] = full_statement[param]
@ -95,10 +95,10 @@ module ImpressionistController
end end
# creates a statment hash that contains default values for creating an impression. # 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!( query_params.reverse_merge!(
:impressionable_type => controller_name.singularize.camelize, :impressionable_type => controller_name.singularize.camelize,
:impressionable_id=> params[:id] :impressionable_id => impressionable.present? ? impressionable.id : params[:id]
) )
associative_create_statement(query_params) associative_create_statement(query_params)
end end

View File

@ -18,9 +18,10 @@ group :test do
gem 'minitest' gem 'minitest'
gem 'minitest-rails' gem 'minitest-rails'
gem 'rails', '~> 3.2.15' gem 'rails', '~> 3.2.15'
gem 'rspec-rails' gem 'rspec-rails', '~> 2.14.0'
gem 'simplecov' gem 'simplecov'
gem 'systemu' gem 'systemu'
gem 'friendly_id', '~> 4.0.9'
end end
gemspec :path => '../' gemspec :path => '../'

View File

@ -18,9 +18,10 @@ group :test do
gem 'minitest' gem 'minitest'
gem 'minitest-rails' gem 'minitest-rails'
gem 'rails', '~> 4.0.1' gem 'rails', '~> 4.0.1'
gem 'rspec-rails' gem 'rspec-rails', '~> 2.14.0'
gem 'simplecov' gem 'simplecov'
gem 'systemu' gem 'systemu'
gem 'friendly_id', '~> 5.1.0'
end end
gemspec :path => '../' gemspec :path => '../'

View File

@ -15,3 +15,4 @@
/coverage /coverage
/log/*.log /log/*.log
/tmp /tmp
/.idea

View File

@ -34,7 +34,7 @@ end
group :development, :test do group :development, :test do
gem 'autotest-notification' gem 'autotest-notification'
gem 'rspec-rails' gem 'rspec-rails', '~> 2.14.0'
gem 'spork' gem 'spork'
end end
@ -45,3 +45,4 @@ group :test do
end end
gem 'jquery-rails' gem 'jquery-rails'
gem 'friendly_id', '~> 4.0.9'

View File

@ -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 File

@ -0,0 +1,6 @@
class Profile < ActiveRecord::Base
extend FriendlyId
friendly_id :username, use: :slugged
is_impressionable
end

View File

@ -0,0 +1,3 @@
<h2>User Public Profile</h2>
<p>User Name: <%= @profile.username %></p>

View File

@ -1,3 +1,4 @@
TestApp::Application.routes.draw do TestApp::Application.routes.draw do
resources :articles, :posts, :widgets, :dummy resources :articles, :posts, :widgets, :dummy
get 'profiles/[:id]' => 'profiles#show'
end end

View File

@ -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 File

@ -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 File

@ -11,7 +11,7 @@
# #
# It's strongly recommended to check this file into your version control system. # 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| create_table "articles", :force => true do |t|
t.string "name" t.string "name"
@ -19,6 +19,17 @@ ActiveRecord::Schema.define(:version => 20130719024021) do
t.datetime "updated_at", :null => false t.datetime "updated_at", :null => false
end 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| create_table "impressions", :force => true do |t|
t.string "impressionable_type" t.string "impressionable_type"
t.integer "impressionable_id" t.integer "impressionable_id"
@ -50,6 +61,13 @@ ActiveRecord::Schema.define(:version => 20130719024021) do
t.datetime "updated_at", :null => false t.datetime "updated_at", :null => false
end 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| create_table "widgets", :force => true do |t|
t.string "name" t.string "name"
t.integer "impressions_count", :default => 0 t.integer "impressions_count", :default => 0

View File

@ -306,6 +306,21 @@ describe DummyController do
end 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 shared_examples_for 'an impressionable action' do
it 'should record an impression' do it 'should record an impression' do
controller.impressionist_subapp_filter(condition) controller.impressionist_subapp_filter(condition)

View File

@ -0,0 +1,4 @@
one:
id: 1
username: test_profile
slug: test_profile

View File

@ -1,7 +1,7 @@
require 'spec_helper' require 'spec_helper'
describe Impression do describe Impression do
fixtures :articles,:impressions,:posts fixtures :articles,:impressions,:posts,:profiles
before(:each) do before(:each) do
@article = Article.find(1) @article = Article.find(1)

View File

@ -4,7 +4,7 @@ require 'systemu'
# FIXME this test might break the others if run before them # FIXME this test might break the others if run before them
# started fixing @nbit001 # started fixing @nbit001
describe Impressionist, :migration do describe Impressionist, :migration do
fixtures :articles,:impressions,:posts fixtures :articles,:impressions,:posts,:profiles
it "should delete existing migration and generate the migration file" do it "should delete existing migration and generate the migration file" do
pending pending
migrations_dir = "#{Rails.root}/db/migrate" migrations_dir = "#{Rails.root}/db/migrate"