Module: Backframe::ActsAsPercent::ClassMethods

Defined in:
lib/backframe/activerecord/acts_as_percent.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_percent(entity) ⇒ Object



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
# File 'lib/backframe/activerecord/acts_as_percent.rb', line 14

def acts_as_percent(entity)

  class_eval <<-EOV

    after_initialize :uncast_percent_#{entity}, :if => Proc.new { |c| c.#{entity}.present? }
    before_save :cast_percent_#{entity}, :if => Proc.new { |c| c.#{entity}.present? }
    after_save :uncast_percent_#{entity}, :if => Proc.new { |c| c.#{entity}.present? }

    def raw_#{entity}
      self.#{entity} / 100.00
    end

    private

    def uncast_percent_#{entity}
      self.#{entity} = self.#{entity} * 100.00
    end

    def cast_percent_#{entity}
      self.#{entity} = self.#{entity} / 100.00
    end

  EOV

end