FIX #103, changed constantize for safe_constantize
This commit is contained in:
parent
3839dbbf41
commit
c9ae78d92a
|
@ -8,3 +8,5 @@
|
||||||
/test_app/test
|
/test_app/test
|
||||||
/test_app/vendor
|
/test_app/vendor
|
||||||
Gemfile.lock
|
Gemfile.lock
|
||||||
|
*.swo
|
||||||
|
*.swp
|
||||||
|
|
|
@ -1,10 +1,3 @@
|
||||||
# Note
|
|
||||||
# It is only updatable if
|
|
||||||
# impressionist_id && impressionist_type(class) are present &&
|
|
||||||
# impressionable_class(which is imp_type.constantize) is counter_caching
|
|
||||||
# Defined like so
|
|
||||||
# is_impressionable :counter_cache => true
|
|
||||||
|
|
||||||
module Impressionist
|
module Impressionist
|
||||||
module CounterCache
|
module CounterCache
|
||||||
|
|
||||||
|
@ -12,21 +5,39 @@ module Impressionist
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
# if updatable returns true, it must be qualified to update_counters
|
# A valid impression must
|
||||||
# impressionable_class instance var is set when updatable? is called
|
# have a valid impressionable class
|
||||||
|
# be counter_caching
|
||||||
|
# have a record saved in the db
|
||||||
|
# then it should give it a try
|
||||||
def impressionable_counter_cache_updatable?
|
def impressionable_counter_cache_updatable?
|
||||||
updatable? && impressionable_try
|
updatable? && impressionable_try
|
||||||
end
|
end
|
||||||
|
|
||||||
# asks imp_id && imp_class whether it's present or not
|
|
||||||
# so that it is updatable
|
|
||||||
def updatable?
|
def updatable?
|
||||||
@impressionable_class = impressionable_class_set
|
valid_impressionable_class? && impressionable_find
|
||||||
impressionable_valid?
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def impressionable_valid?
|
def valid_impressionable_class?
|
||||||
(self.impressionable_id.present? && impressionable_class)
|
set_impressionable_class && counter_caching?
|
||||||
|
end
|
||||||
|
|
||||||
|
def set_impressionable_class
|
||||||
|
klass = self.impressionable_type || false
|
||||||
|
@impressionable_class = klass.
|
||||||
|
to_s.safe_constantize || false
|
||||||
|
end
|
||||||
|
|
||||||
|
# default mode is ERROR
|
||||||
|
def impressionist_log(str, mode=:error)
|
||||||
|
Rails.logger.send(mode.to_s, str)
|
||||||
|
end
|
||||||
|
|
||||||
|
# receives an entity(instance of a Model) and then tries to update
|
||||||
|
# counter_cache column
|
||||||
|
# entity is a impressionable instance model
|
||||||
|
def impressionable_try
|
||||||
|
entity.try(:update_impressionist_counter_cache)
|
||||||
end
|
end
|
||||||
|
|
||||||
def impressionable_find
|
def impressionable_find
|
||||||
|
@ -37,29 +48,11 @@ module Impressionist
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# imps_type == nil, constantize returns Object
|
def counter_caching?
|
||||||
# It attemps to return false so it won't be updatable
|
impressionable_class.
|
||||||
# calls to_s otherwise it would try to constantize nil
|
impressionist_counter_caching?
|
||||||
# and it would raise an exeception
|
|
||||||
# Must be !~ Object and be counter_caching to be qualified as updatable
|
|
||||||
def impressionable_class_set
|
|
||||||
klass = self.impressionable_type.to_s.constantize
|
|
||||||
(klass.to_s !~ /Object/) && klass.impressionist_counter_caching? ? klass : false
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# default mode is ERROR
|
|
||||||
# ruby 1.8.7 support
|
|
||||||
def impressionist_log(str, mode=:error)
|
|
||||||
Rails.logger.send(mode.to_s, str)
|
|
||||||
end
|
|
||||||
|
|
||||||
# receives an entity(instance of a Model) and then tries to update
|
|
||||||
# counter_cache column
|
|
||||||
# entity is a impressionable instance model
|
|
||||||
def impressionable_try
|
|
||||||
impressionable_find
|
|
||||||
entity.try(:update_impressionist_counter_cache)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Returns false, as it is only handling one exeception
|
# Returns false, as it is only handling one exeception
|
||||||
# It would make updatable to fail thereafter it would not try
|
# It would make updatable to fail thereafter it would not try
|
||||||
|
|
|
@ -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 => 20130709003307) do
|
ActiveRecord::Schema.define(:version => 20130719024021) do
|
||||||
|
|
||||||
create_table "articles", :force => true do |t|
|
create_table "articles", :force => true do |t|
|
||||||
t.string "name"
|
t.string "name"
|
||||||
|
|
Loading…
Reference in New Issue