Refactor: extract methods to a module and class helper, dollarized_attribute
This commit is contained in:
1 parent
83e3023d85
commit
56a38fbd93
5 files changed
+31
-31
No files matched your search
@@ -21,6 +21,8 @@ class Deliverable < ActiveRecord::Base
|
|||||||
validates_presence_of :manager
|
validates_presence_of :manager
|
||||||
|
|
||||||
# Accessors
|
# Accessors
|
||||||
|
include DollarizedAttribute
|
||||||
|
dollarized_attribute :total
|
||||||
|
|
||||||
delegate :name, :to => :contract, :prefix => true, :allow_nil => true
|
delegate :name, :to => :contract, :prefix => true, :allow_nil => true
|
||||||
|
|
||||||
@@ -53,14 +55,6 @@ class Deliverable < ActiveRecord::Base
|
|||||||
self.class.to_s.underscore
|
self.class.to_s.underscore
|
||||||
end
|
end
|
||||||
|
|
||||||
def total=(v)
|
|
||||||
if v.is_a? String
|
|
||||||
write_attribute(:total, v.gsub(/[$ ,]/, ''))
|
|
||||||
else
|
|
||||||
write_attribute(:total, v)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def labor_budget_total(date=nil)
|
def labor_budget_total(date=nil)
|
||||||
labor_budgets.sum(:budget)
|
labor_budgets.sum(:budget)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -7,13 +7,8 @@ class FixedBudget < ActiveRecord::Base
|
|||||||
# Validations
|
# Validations
|
||||||
|
|
||||||
# Accessors
|
# Accessors
|
||||||
def budget=(v)
|
include DollarizedAttribute
|
||||||
if v.is_a? String
|
dollarized_attribute :budget
|
||||||
write_attribute(:budget, v.gsub(/[$ ,]/, ''))
|
|
||||||
else
|
|
||||||
write_attribute(:budget, v)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
named_scope :by_period, lambda {|date|
|
named_scope :by_period, lambda {|date|
|
||||||
if date
|
if date
|
||||||
|
|||||||
@@ -7,12 +7,6 @@ class LaborBudget < ActiveRecord::Base
|
|||||||
# Validations
|
# Validations
|
||||||
|
|
||||||
# Accessors
|
# Accessors
|
||||||
|
include DollarizedAttribute
|
||||||
def budget=(v)
|
dollarized_attribute :budget
|
||||||
if v.is_a? String
|
|
||||||
write_attribute(:budget, v.gsub(/[$ ,]/, ''))
|
|
||||||
else
|
|
||||||
write_attribute(:budget, v)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
@@ -7,12 +7,6 @@ class OverheadBudget < ActiveRecord::Base
|
|||||||
# Validations
|
# Validations
|
||||||
|
|
||||||
# Accessors
|
# Accessors
|
||||||
|
include DollarizedAttribute
|
||||||
def budget=(v)
|
dollarized_attribute :budget
|
||||||
if v.is_a? String
|
|
||||||
write_attribute(:budget, v.gsub(/[$ ,]/, ''))
|
|
||||||
else
|
|
||||||
write_attribute(:budget, v)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
# Shared module to allow seting an attribute using:
|
||||||
|
# * Dollar amount - $1,000.00
|
||||||
|
# * Number - 100.00
|
||||||
|
module DollarizedAttribute
|
||||||
|
module ClassMethods
|
||||||
|
|
||||||
|
# dollarized_attribute(:budget) will create a budget=(value) method
|
||||||
|
def dollarized_attribute(attribute)
|
||||||
|
define_method(attribute.to_s + '=') {|value|
|
||||||
|
if value.is_a? String
|
||||||
|
write_attribute(attribute, value.gsub(/[$ ,]/, ''))
|
||||||
|
else
|
||||||
|
write_attribute(attribute, value)
|
||||||
|
end
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.included(base)
|
||||||
|
base.extend ClassMethods
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in new issue
Block a user