Unique impressionist count for objects which are using friendly_id

This commit is contained in:
Md. Tauhidul Islam 2015-02-07 22:08:32 +06:00
parent cacdd66fd8
commit f77d187f66
14 changed files with 103 additions and 9 deletions

View File

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

View File

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

View File

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

View File

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

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
resources :articles, :posts, :widgets, :dummy
get 'profiles/[:id]' => 'profiles#show'
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.
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

View File

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

View File

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

View File

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

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