lib/catwalk/presenter.rb in catwalk-0.0.1 vs lib/catwalk/presenter.rb in catwalk-0.0.2
- old
+ new
@@ -6,22 +6,31 @@
def initialize(model)
@model = model
end
- define_method(:to_model) { @model }
- define_method(:to_param) { @model.to_param if persisted? }
- define_method(:to_key) { @model.to_key if persisted? }
- define_method(:persisted?) { @model.persisted? }
- define_method(:valid?) { @model.valid? }
- define_method(:model_name) { @model.model_name }
- define_method(:errors) { @model.errors }
+ def to_model; @model; end
+ def to_param; @model.to_param if persisted?; end
+ def to_key; @model.to_key if persisted?; end
+ def persisted?; @model.persisted?; end
+ def valid?; @model.valid?; end
+ def model_name; @model.model_name; end
+ def errors; @model.errors; end
module ClassMethods
- def field(field_name)
+ def field(field_name,opts={})
define_method field_name do
- @model.send field_name
+ field_value = @model.send field_name
+ default_when = opts[:default_when]
+
+ if default_when && field_value.send(default_when)
+ "Not Set"
+ elsif opts[:format_with] && opts[:format_with].respond_to?(:call)
+ opts[:format_with].call field_value
+ else
+ field_value
+ end
end
end
def access_as(accessor)
define_method accessor do