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
57 58 59 |
# File 'app/models/type_station/entity.rb', line 57 def content?(key) content_attributes[key].present? end |
#content_attributes ⇒ Object
31 32 33 |
# File 'app/models/type_station/entity.rb', line 31 def content_attributes @content_attributes ||= Hash[self.contents.map {|c| [c.name, c]}] end |
#entity_fields?(key) ⇒ Boolean
61 62 63 |
# File 'app/models/type_station/entity.rb', line 61 def entity_fields?(key) entity_fields.include?(key) end |
#get(key) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'app/models/type_station/entity.rb', line 35 def get(key) if entity_fields?(key) self[key] elsif content?(key) content_attributes[key].get else nil end end |
#move_entity(direction) ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'app/models/type_station/entity.rb', line 65 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
45 46 47 48 49 50 51 52 53 54 55 |
# File 'app/models/type_station/entity.rb', line 45 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 29 |
# File 'app/models/type_station/entity.rb', line 21 def update_contents(params) params.each do |data| d = data.to_unsafe_h field, value, type = d[:field].to_sym, d[:value], d[:type].to_sym set(field, value, type) end save end |