Module: Effective::Resources::Attributes
- Included in:
- Effective::Resource
- Defined in:
- app/models/effective/resources/attributes.rb
Instance Method Summary collapse
- #attribute_names ⇒ Object
- #attributes ⇒ Object
- #belong_tos_attributes ⇒ Object
-
#klass_attributes(all: false) ⇒ Object
All attributes from the klass, sorted as per attributes block.
- #written_attributes ⇒ Object
Instance Method Details
#attribute_names ⇒ Object
9 10 11 |
# File 'app/models/effective/resources/attributes.rb', line 9 def attribute_names attributes.map { |attribute| attribute.name } end |
#attributes ⇒ Object
5 6 7 |
# File 'app/models/effective/resources/attributes.rb', line 5 def attributes (klass_attributes.presence || written_attributes.presence) end |
#belong_tos_attributes ⇒ Object
33 34 35 36 37 38 39 |
# File 'app/models/effective/resources/attributes.rb', line 33 def belong_tos_attributes belong_tos.map do |reference| unless reference.foreign_key == 'site_id' && klass.respond_to?(:acts_as_site_specific) Effective::Attribute.new(reference.foreign_key, :integer) end end.compact.sort end |
#klass_attributes(all: false) ⇒ Object
All attributes from the klass, sorted as per attributes block. Does not include :id, :created_at, :updated_at unless all is passed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'app/models/effective/resources/attributes.rb', line 15 def klass_attributes(all: false) attributes = (klass.new().attributes rescue nil) return [] unless attributes names = attributes.keys - belong_tos.map { |reference| reference.foreign_key } names = names - [klass.primary_key, 'created_at', 'updated_at'] unless all attributes = names.map do |name| if klass.respond_to?(:column_for_attribute) # Rails 4+ Effective::Attribute.new(name, klass.column_for_attribute(name).type) else Effective::Attribute.new(name, klass.columns_hash[name].type) end end sort_by_written_attributes(attributes) end |
#written_attributes ⇒ Object
41 42 43 44 |
# File 'app/models/effective/resources/attributes.rb', line 41 def written_attributes _initialize_written if @written_attributes.nil? @written_attributes end |