Module: Granite::Form::Model::Attributes::ClassMethods
- Defined in:
- lib/granite/form/model/attributes.rb
Instance Method Summary collapse
- #add_attribute(reflection_class, *args, &block) ⇒ Object
- #alias_attribute(alias_name, attribute_name) ⇒ Object
- #attribute_names(include_associations = true) ⇒ Object
- #dirty? ⇒ Boolean
-
#has_attribute?(name) ⇒ Boolean
rubocop:disable Naming/PredicateName.
- #inspect ⇒ Object
- #reflect_on_attribute(name) ⇒ Object
- #with_sanitize(value) ⇒ Object
Instance Method Details
#add_attribute(reflection_class, *args, &block) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/granite/form/model/attributes.rb', line 38 def add_attribute(reflection_class, *args, &block) reflection = reflection_class.build(self, generated_attributes_methods, *args, &block) self._attributes = _attributes.merge(reflection.name => reflection) should_define_dirty = dirty? && reflection_class != Granite::Form::Model::Attributes::Reflections::Base define_dirty(reflection.name, generated_attributes_methods) if should_define_dirty reflection end |
#alias_attribute(alias_name, attribute_name) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/granite/form/model/attributes.rb', line 46 def alias_attribute(alias_name, attribute_name) reflection = reflect_on_attribute(attribute_name) raise ArgumentError, "Unable to alias undefined attribute `#{attribute_name}` on #{self}" unless reflection if reflection.class == Granite::Form::Model::Attributes::Reflections::Base raise ArgumentError, "Unable to alias base attribute `#{attribute_name}`" end reflection.class.generate_methods alias_name, generated_attributes_methods self._attribute_aliases = _attribute_aliases.merge(alias_name.to_s => reflection.name) define_dirty alias_name, generated_attributes_methods if dirty? reflection end |
#attribute_names(include_associations = true) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/granite/form/model/attributes.rb', line 71 def attribute_names(include_associations = true) if include_associations _attributes.keys else _attributes.map do |name, attribute| name unless attribute.class == Granite::Form::Model::Attributes::Reflections::Base end.compact end end |
#dirty? ⇒ Boolean
85 86 87 |
# File 'lib/granite/form/model/attributes.rb', line 85 def dirty? false end |
#has_attribute?(name) ⇒ Boolean
rubocop:disable Naming/PredicateName
66 67 68 69 |
# File 'lib/granite/form/model/attributes.rb', line 66 def has_attribute?(name) # rubocop:disable Naming/PredicateName name = name.to_s _attributes.key?(_attribute_aliases[name] || name) end |
#inspect ⇒ Object
81 82 83 |
# File 'lib/granite/form/model/attributes.rb', line 81 def inspect "#{original_inspect}(#{attributes_for_inspect.presence || 'no attributes'})" end |
#reflect_on_attribute(name) ⇒ Object
61 62 63 64 |
# File 'lib/granite/form/model/attributes.rb', line 61 def reflect_on_attribute(name) name = name.to_s _attributes[_attribute_aliases[name] || name] end |
#with_sanitize(value) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/granite/form/model/attributes.rb', line 89 def with_sanitize(value) previous_sanitize = _sanitize self._sanitize = value yield ensure self._sanitize = previous_sanitize end |