merged/app/models/merged/option_definition.rb

42 lines
1.2 KiB
Ruby

module Merged
# Quite simple mapping from name to usually a selection of values.
# Options have type that can be set but usually is inferred from
# wether there is one values (select) other wise text.
# date is also supported.
#
# a corresponding ./app/views/merged/sections/_option_form_TYPE.haml
# nust exist to render the type.
#
# Also values may be provided by a module function/attribute on Merged.
# eg option text_color (also shade_color) have a Merged.text_color that return a
# hash. In this case so it is easy to customize the colors of the app.
#
class OptionDefinition < SharedBase
fields :name , :default , :description , :values , :type
def type
return attributes[:type] unless attributes[:type].blank?
if has_values?
"select"
else
"text"
end
end
def has_values?
return true if Merged.respond_to?( name.to_sym )
return false if attributes[:values].nil?
! attributes[:values].empty?
end
def values
return Merged.send(name.to_sym).keys if Merged.respond_to?( name.to_sym )
return [] unless has_values?
attributes[:values].split(" ")
end
end
end