Class: TypeStation::Entity
- Inherits:
-
Object
- Object
- TypeStation::Entity
- Includes:
- Mongoid::Document, Mongoid::Tree, Mongoid::Tree::Ordering
- Defined in:
- app/models/type_station/entity.rb
Direct Known Subclasses
Class Method Summary collapse
-
.find_by_name(name) ⇒ Object
CLASS METHODS.
Instance Method Summary collapse
- #content?(key) ⇒ Boolean
- #content_attributes ⇒ Object
- #entity_fields?(key) ⇒ Boolean
- #get(key) ⇒ Object
- #move_entity(direction) ⇒ Object
- #set(key, value, type = 'text') ⇒ Object
-
#update_contents(params) ⇒ Object
INSTANT METHODS.
Class Method Details
.find_by_name(name) ⇒ Object
CLASS METHODS
15 16 17 |
# File 'app/models/type_station/entity.rb', line 15 def self.find_by_name(name) self.where(name: name).first end |
Instance Method Details
#content?(key) ⇒ Boolean
56 57 58 |
# File 'app/models/type_station/entity.rb', line 56 def content?(key) content_attributes[key].present? end |
#content_attributes ⇒ Object
30 31 32 |
# File 'app/models/type_station/entity.rb', line 30 def content_attributes @content_attributes ||= Hash[self.contents.map {|c| [c.name, c]}] end |
#entity_fields?(key) ⇒ Boolean
60 61 62 |
# File 'app/models/type_station/entity.rb', line 60 def entity_fields?(key) entity_fields.include?(key) end |
#get(key) ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'app/models/type_station/entity.rb', line 34 def get(key) if entity_fields?(key) self[key] elsif content?(key) content_attributes[key].get else nil end end |
#move_entity(direction) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'app/models/type_station/entity.rb', line 64 def move_entity(direction) case direction.to_sym when :move_up move_up when :move_down move_down end end |
#set(key, value, type = 'text') ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'app/models/type_station/entity.rb', line 44 def set(key, value, type = 'text') if entity_fields?(key) && !changed.include?(key) #and not changed already self[key] = value else if content?(key) content_attributes[key].set value else contents.build(name: key, type: type).set(value) end end end |
#update_contents(params) ⇒ Object
INSTANT METHODS
21 22 23 24 25 26 27 28 |
# File 'app/models/type_station/entity.rb', line 21 def update_contents(params) params.each do |data| field, value, type = data[:field].to_sym, data[:value], data[:type].to_sym set(field, value, type) end save end |