Module: Persistize::ActiveRecord::ClassMethods

Defined in:
lib/persistize/active_record.rb

Instance Method Summary collapse

Instance Method Details

#persistize(*args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/persistize/active_record.rb', line 7

def persistize(*args)
  options = args.extract_options!

  performant = {:performant => ::Persistize.performant}.merge(options)[:performant]

  args.each do |method|
    attribute = method.to_s.sub(/\?$/, '')

    original_method = :"_unpersistized_#{attribute}"
    update_method   = :"_update_#{attribute}"

    class_eval "      alias \#{original_method} \#{method}                    # alias _unpersistized_full_name full_name\n                                                            #\n      def \#{method}                                         # def full_name\n        if new_record? || changed?                          #   if new_record? || changed?\n          \#{original_method}                                #     _unpersistized_full_name\n        else                                                #   else\n          self[:\#{attribute}]                               #     self[:full_name]\n        end                                                 #   end\n      end                                                   # end\n                                                            #\n      before_create :\#{update_method}                       # before_create :_update_full_name\n      before_update :\#{update_method}, :if => :changed?     # before_update :_update_full_name, :if => :changed?\n                                                            #\n      def \#{update_method}                                  # def _update_full_name\n        self[:\#{attribute}] = \#{original_method}            #   self[:full_name] = _unpersistized_full_name\n        true # return true to avoid canceling the save      #   true\n      end                                                   # end\n                                                            #\n      def \#{update_method}!                                 # def _update_full_name!\n        if \#{performant}                                    #   if performant\n          new_\#{attribute} = \#{original_method}             #     new_full_name = _unpersistized_full_name\n          return if new_\#{attribute} == self[:\#{attribute}] #     return if new_full_name == self[:full_name]\n          update_attribute :\#{attribute}, new_\#{attribute}  #     update_attribute :full_name, new_full_name\n        else                                                #   else\n          \#{update_method}                                  #     _update_full_name\n          save! if \#{attribute}_changed?                    #     save! if full_name_changed?\n        end                                                 #   end\n      end                                                   # end\n    RUBY\n\n    if options && options[:depending_on]\n      dependencies = [options[:depending_on]].flatten\n\n      dependencies.each do |dependency|\n        generate_callback(reflections[dependency], update_method)\n      end\n    end\n\n  end\nend\n", __FILE__, __LINE__ + 1