Module: Effective::Resources::Attributes

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

Instance Method Summary collapse

Instance Method Details

#attribute_namesObject



9
10
11
# File 'app/models/effective/resources/attributes.rb', line 9

def attribute_names
  attributes.map { |attribute| attribute.name }
end

#attributesObject



5
6
7
# File 'app/models/effective/resources/attributes.rb', line 5

def attributes
  (klass_attributes.presence || written_attributes.presence)
end

#belong_tos_attributesObject



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_attributesObject



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