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 <<-RUBY, __FILE__, __LINE__ + 1
      alias #{original_method} #{method}                    # alias _unpersistized_full_name full_name
                                                            #
      def #{method}                                         # def full_name
        if new_record? || changed?                          #   if new_record? || changed?
          #{original_method}                                #     _unpersistized_full_name
        else                                                #   else
          self[:#{attribute}]                               #     self[:full_name]
        end                                                 #   end
      end                                                   # end
                                                            #
      before_create :#{update_method}                       # before_create :_update_full_name
      before_update :#{update_method}, :if => :changed?     # before_update :_update_full_name, :if => :changed?
                                                            #
      def #{update_method}                                  # def _update_full_name
        self[:#{attribute}] = #{original_method}            #   self[:full_name] = _unpersistized_full_name
        true # return true to avoid canceling the save      #   true
      end                                                   # end
                                                            #
      def #{update_method}!                                 # def _update_full_name!
        if #{performant}                                    #   if performant
          new_#{attribute} = #{original_method}             #     new_full_name = _unpersistized_full_name
          return if new_#{attribute} == self[:#{attribute}] #     return if new_full_name == self[:full_name]
          update_attribute :#{attribute}, new_#{attribute}  #     update_attribute :full_name, new_full_name
        else                                                #   else
          #{update_method}                                  #     _update_full_name
          save! if #{attribute}_changed?                    #     save! if full_name_changed?
        end                                                 #   end
      end                                                   # end
    RUBY

    if options && options[:depending_on]
      dependencies = [options[:depending_on]].flatten

      dependencies.each do |dependency|
        generate_callback(reflections[dependency], update_method)
      end
    end

  end
end