Created
October 30, 2011 11:04
-
-
Save Talleyran/1325792 to your computer and use it in GitHub Desktop.
модель FieldsObject
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FieldsObject | |
include Mongoid::Document | |
include Mongoid::Timestamps | |
@@field_types = [ | |
:array, | |
:big_decimal, | |
:boolean, | |
:date, | |
:date_time, | |
:float, | |
:hash, | |
:integer, | |
:range, | |
:string, | |
:symbol, | |
:time, | |
] | |
attr_accessor :field_type, :value | |
embedded_in :layers_object | |
field :key, type: Symbol | |
field :field_type_code, type: Integer | |
field :array_value, type: Array | |
field :big_decimal_value, type: BigDecimal | |
field :boolean_value, type: Boolean | |
field :date_value, type: Date | |
field :date_time_value, type: DateTime | |
field :float_value, type: Float | |
field :hash_value, type: Hash | |
field :integer_value, type: Integer | |
field :range_value, type: Range | |
field :string_value, type: String | |
field :symbol_value, type: Symbol | |
field :time_value, type: Time | |
before_validation :set_fields | |
after_save :clear_temp_attributes | |
def value | |
if new_record? | |
@value ||= attributes["value"] | |
else | |
@value ||= self.send(value_field.to_sym) | |
end | |
end | |
def field_type | |
if new_record? | |
@field_type ||= attributes["field_type"] | |
else | |
@field_type ||= @@field_types[field_type_code] | |
end | |
end | |
private | |
def value_field | |
"#{field_type}_value" | |
end | |
def set_fields | |
self.send("#{ value_field }=".to_sym, value) | |
self.field_type_code = @@field_types.index(field_type) if new_record? | |
end | |
def clear_temp_attributes | |
@value = nil | |
@field_type = nil | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment