Class: Katapult::Model
- 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
-
#attrs ⇒ Object
Returns the value of attribute attrs.
-
#belongs_tos ⇒ Object
Returns the value of attribute belongs_tos.
-
#has_manys ⇒ Object
Returns the value of attribute has_manys.
Attributes inherited from Element
#application_model, #name, #options
Instance Method Summary collapse
- #add_foreign_key_attrs(belongs_tos) ⇒ Object
-
#attr(attr_name, options = {}) ⇒ Object
DSL.
-
#belongs_to(model_name) ⇒ Object
DSL.
- #db_fields ⇒ Object
- #editable_attrs ⇒ Object
-
#initialize(*args) ⇒ Model
constructor
A new instance of Model.
- #label_attr ⇒ Object
- #label_attr? ⇒ Boolean
- #render(options = {}) ⇒ Object
- #renderable_attrs ⇒ Object
- #required_attrs ⇒ Object
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
#attrs ⇒ Object
Returns the value of attribute attrs.
13 14 15 |
# File 'lib/katapult/elements/model.rb', line 13 def attrs @attrs end |
#belongs_tos ⇒ Object
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_manys ⇒ Object
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, = {}) [:model] = self attrs << Attribute.new(attr_name, ) 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_fields ⇒ Object
46 47 48 |
# File 'lib/katapult/elements/model.rb', line 46 def db_fields attrs.reject(&:skip_db) end |
#editable_attrs ⇒ Object
54 55 56 |
# File 'lib/katapult/elements/model.rb', line 54 def editable_attrs attrs.select &:editable? end |
#label_attr ⇒ Object
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
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( = {}) Generators::ModelGenerator.new(self, ).invoke_all end |
#renderable_attrs ⇒ Object
50 51 52 |
# File 'lib/katapult/elements/model.rb', line 50 def renderable_attrs attrs.select &:renderable? end |
#required_attrs ⇒ Object
58 59 60 |
# File 'lib/katapult/elements/model.rb', line 58 def required_attrs attrs.select &:required? end |