Module: Effective::Resources::Attributes

Included in:
Effective::Resource
Defined in:
app/models/effective/resources/attributes.rb

Instance Method Summary collapse

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



30
31
32
33
34
35
36
# File 'app/models/effective/resources/attributes.rb', line 30

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 ⇒ Object

All attributes from the klass, sorted as per attributes block. Does not include :id, :created_at, :updated_at



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/models/effective/resources/attributes.rb', line 15

def klass_attributes
  attributes = (klass.new().attributes rescue nil)
  return [] unless attributes

  attributes = (attributes.keys - [klass.primary_key, 'created_at', 'updated_at'] - belong_tos.map { |reference| reference.foreign_key }).map do |att|
    if klass.respond_to?(:column_for_attribute) # Rails 4+
      Effective::Attribute.new(att, klass.column_for_attribute(att).try(:type))
    else
      Effective::Attribute.new(att, klass.columns_hash[att].try(:type))
    end
  end

  sort(attributes)
end

#written_attributes ⇒ Object



38
39
40
41
# File 'app/models/effective/resources/attributes.rb', line 38

def written_attributes
  _initialize_written if @written_attributes.nil?
  @written_attributes
end