Class: TypeStation::Entity

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Tree, Mongoid::Tree::Ordering
Defined in:
app/models/type_station/entity.rb

Direct Known Subclasses

Page

Class Method Summary collapse

Instance Method Summary collapse

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

Returns:

  • (Boolean)


57
58
59
# File 'app/models/type_station/entity.rb', line 57

def content?(key)
  content_attributes[key].present?
end

#content_attributesObject



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

Returns:

  • (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