FIX #103, changed constantize for safe_constantize

This commit is contained in:
Antonio C Nalesso Moreira 2013-08-31 00:28:20 +01:00
parent 3839dbbf41
commit c9ae78d92a
3 changed files with 33 additions and 38 deletions

2
.gitignore vendored
View File

@ -8,3 +8,5 @@
/test_app/test
/test_app/vendor
Gemfile.lock
*.swo
*.swp

View File

@ -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 CounterCache
@ -12,21 +5,39 @@ module Impressionist
private
# if updatable returns true, it must be qualified to update_counters
# impressionable_class instance var is set when updatable? is called
# A valid impression must
# 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?
updatable? && impressionable_try
updatable? && impressionable_try
end
# asks imp_id && imp_class whether it's present or not
# so that it is updatable
def updatable?
@impressionable_class = impressionable_class_set
impressionable_valid?
valid_impressionable_class? && impressionable_find
end
def impressionable_valid?
(self.impressionable_id.present? && impressionable_class)
def valid_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
def impressionable_find
@ -37,29 +48,11 @@ module Impressionist
end
# imps_type == nil, constantize returns Object
# It attemps to return false so it won't be updatable
# calls to_s otherwise it would try to constantize nil
# 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
def counter_caching?
impressionable_class.
impressionist_counter_caching?
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
# It would make updatable to fail thereafter it would not try

View File

@ -11,7 +11,7 @@
#
# 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|
t.string "name"