Module: Effective::Resources::Attributes

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

Instance Method Summary collapse

Instance Method Details

#active_storage_attributesObject



48
49
50
51
52
53
# File 'app/models/effective/resources/attributes.rb', line 48

def active_storage_attributes
  {}.tap do |retval|
    active_storage_has_ones_ids.each { |k, v| retval[k] = [:string] }
    active_storage_has_manys_ids.each { |k, v| retval[k] = [:array] }
  end
end

#active_text_attributesObject



55
56
57
58
59
# File 'app/models/effective/resources/attributes.rb', line 55

def active_text_attributes
  {}.tap do |retval|
    action_texts_has_ones_ids.each { |k, v| retval[k] = [:string] }
  end
end

#attributesObject

This is the attributes as defined by ActiveRecord table { :name => [:string], … }



7
8
9
# File 'app/models/effective/resources/attributes.rb', line 7

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

#belong_tos_attributesObject

The attributes for each belongs_to { :client_id => [:integer], … }



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

def belong_tos_attributes
  belong_tos.inject({}) do |h, ass|
    unless ass.foreign_key == 'site_id' && ass.respond_to?(:acts_as_site_specific)
      h[ass.foreign_key.to_sym] = [:integer, :index => true]

      if ass.options[:polymorphic]
        h[ass.foreign_type.to_sym] = [:string, :index => true]
      end

    end; h
  end
end

#effective_addresses_attributesObject



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

def effective_addresses_attributes
  return {} unless defined?(EffectiveAddresses) && instance.respond_to?(:effective_address_names)
  instance.effective_address_names.inject({}) { |h, name| h[name] = [:effective_address]; h }
end

#effective_assets_attributesObject



43
44
45
46
# File 'app/models/effective/resources/attributes.rb', line 43

def effective_assets_attributes
  return {} unless defined?(EffectiveAssets) && instance.respond_to?(:asset_boxes)
  { effective_assets: [:effective_assets] }
end

#has_manys_attributesObject



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

def has_manys_attributes
  has_manys_ids.inject({}) { |h, ass| h[ass] = [:array]; h }
end

#has_ones_attributesObject



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

def has_ones_attributes
  has_ones_ids.inject({}) { |h, ass| h[ass] = [:array]; h }
end

#klass_attributes(all: false, sort: false) ⇒ Object

All attributes from the klass, sorted as per model attributes block. Does not include :id, :created_at, :updated_at unless all is passed



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/models/effective/resources/attributes.rb', line 104

def klass_attributes(all: false, sort: false)
  attributes = (klass.new().attributes rescue nil)
  return [] unless attributes

  names = attributes.keys
  names -= belong_tos.map { |reference| reference.foreign_key }
  names -= belong_tos.map { |reference| reference.foreign_type if reference.options[:polymorphic] }
  names -= [klass.primary_key, 'created_at', 'updated_at'] unless all

  attributes = names.inject({}) do |h, name|
    if klass.respond_to?(:column_for_attribute) # Rails 4+
      h[name.to_sym] = [klass.column_for_attribute(name).type]
    else
      h[name.to_sym] = [klass.columns_hash[name].type]
    end; h
  end

  sort ? sort_by_model_attributes(attributes) : attributes
end

#model_attributes(all: false) ⇒ Object

All will include primary_key, created_at, updated_at and belongs_tos This is the attributes as defined by the effective_resources do .. end block { :name => [:string, { permitted: false }], … }



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/effective/resources/attributes.rb', line 64

def model_attributes(all: false)
  atts = (model ? model.attributes : {})

  if all # Probably being called by permitted_attributes
    primary_key_attribute
      .merge(belong_tos_attributes)
      .merge(has_manys_attributes)
      .merge(has_ones_attributes)
      .merge(effective_addresses_attributes)
      .merge(effective_assets_attributes)
      .merge(active_storage_attributes)
      .merge(active_text_attributes)
      .merge(atts)
  else  # This is the migrator. This should match table_attributes
    belong_tos_attributes.merge(atts.reject { |_, v| v[0] == :permitted_param })
  end
end

#permitted_attributesObject

Used by effective_crud_controller to generate the permitted params



98
99
100
# File 'app/models/effective/resources/attributes.rb', line 98

def permitted_attributes
  model_attributes(all: true)
end

#primary_key_attributeObject



11
12
13
# File 'app/models/effective/resources/attributes.rb', line 11

def primary_key_attribute
  {klass.primary_key.to_sym => [:integer]}
end

#table_attributesObject

All table attributes. includes primary_key and belongs_tos



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/effective/resources/attributes.rb', line 83

def table_attributes
  attributes = (klass.new().attributes rescue nil)
  return {} unless attributes

  (attributes.keys - [klass.primary_key]).inject({}) do |h, name|
    if klass.respond_to?(:column_for_attribute) # Rails 4+
      column = klass.column_for_attribute(name)
      h[name.to_sym] = [column.type] if column.table_name # Rails 5 attributes API
    else
      h[name.to_sym] = [klass.columns_hash[name].type]
    end; h
  end
end