Class: Katapult::Model

Inherits:
Element show all
Defined in:
lib/katapult/elements/model.rb

Constant Summary collapse

UnknownAttributeError =
Class.new(StandardError)
MissingLabelAttributeError =
Class.new(StandardError)

Constants inherited from Element

Element::UnknownFormattingError, Element::UnknownOptionError

Instance Attribute Summary collapse

Attributes inherited from Element

#application_model, #name, #options

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Model

Returns a new instance of Model.



15
16
17
18
19
20
21
22
# File 'lib/katapult/elements/model.rb', line 15

def initialize(*args)
  self.attrs = []
  self._belongs_tos = []
  self.belongs_tos = []
  self.has_manys = []

  super
end

Instance Attribute Details

#attrsObject

Returns the value of attribute attrs.



13
14
15
# File 'lib/katapult/elements/model.rb', line 13

def attrs
  @attrs
end

#belongs_tosObject

Returns the value of attribute belongs_tos.



13
14
15
# File 'lib/katapult/elements/model.rb', line 13

def belongs_tos
  @belongs_tos
end

#has_manysObject

Returns the value of attribute has_manys.



13
14
15
# File 'lib/katapult/elements/model.rb', line 13

def has_manys
  @has_manys
end

Instance Method Details

#add_foreign_key_attrs(belongs_tos) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/katapult/elements/model.rb', line 62

def add_foreign_key_attrs(belongs_tos)
  belongs_tos.each do |other_model|
    attr "#{ other_model.name :variable }_id", type: :foreign_key,
      assignable_values: "#{ other_model.name(:class) }.all.to_a",
      allow_blank: true,
      associated_model: other_model
  end
end

#attr(attr_name, options = {}) ⇒ Object

DSL



25
26
27
28
# File 'lib/katapult/elements/model.rb', line 25

def attr(attr_name, options = {})
  options[:model] = self
  attrs << Attribute.new(attr_name, options)
end

#belongs_to(model_name) ⇒ Object

DSL



31
32
33
# File 'lib/katapult/elements/model.rb', line 31

def belongs_to(model_name)
  application_model.association name, belongs_to: model_name
end

#db_fieldsObject



46
47
48
# File 'lib/katapult/elements/model.rb', line 46

def db_fields
  attrs.reject(&:skip_db)
end

#editable_attrsObject



54
55
56
# File 'lib/katapult/elements/model.rb', line 54

def editable_attrs
  attrs.select &:editable?
end

#label_attrObject



36
37
38
# File 'lib/katapult/elements/model.rb', line 36

def label_attr
  renderable_attrs.first.presence or raise MissingLabelAttributeError
end

#label_attr?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
# File 'lib/katapult/elements/model.rb', line 40

def label_attr?
  label_attr.present?
rescue MissingLabelAttributeError
  false
end

#render(options = {}) ⇒ Object



71
72
73
# File 'lib/katapult/elements/model.rb', line 71

def render(options = {})
  Generators::ModelGenerator.new(self, options).invoke_all
end

#renderable_attrsObject



50
51
52
# File 'lib/katapult/elements/model.rb', line 50

def renderable_attrs
  attrs.select &:renderable?
end

#required_attrsObject



58
59
60
# File 'lib/katapult/elements/model.rb', line 58

def required_attrs
  attrs.select &:required?
end