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



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

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



61
62
63
64
65
# File 'app/models/effective/resources/attributes.rb', line 61

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], … }



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

def attributes
  klass_attributes.presence || model_attributes.presence
end

#belong_tos_attributesObject

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



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

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

#belong_tos_names_attributesObject



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

def belong_tos_names_attributes
  belong_tos.inject({}) { |h, ass| h[ass.name] = [:belongs_to]; h }
end

#effective_addresses_attributesObject



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

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



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

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

#has_manys_attributesObject



36
37
38
# File 'app/models/effective/resources/attributes.rb', line 36

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

#has_ones_attributesObject



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

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



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/models/effective/resources/attributes.rb', line 111

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 }], … }



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/effective/resources/attributes.rb', line 70

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



105
106
107
# File 'app/models/effective/resources/attributes.rb', line 105

def permitted_attributes
  model_attributes(all: true)
end

#primary_key_attributeObject



13
14
15
# File 'app/models/effective/resources/attributes.rb', line 13

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

#resource_attributesObject

Similar to klass_attributes Used by table_builder



133
134
135
136
137
138
# File 'app/models/effective/resources/attributes.rb', line 133

def resource_attributes
  belong_tos_names_attributes
    .merge(klass_attributes(sort: true))
    .merge(effective_addresses_attributes)
    .merge(active_storage_attributes.transform_values { |thing| [:active_storage] })
end

#table_attributesObject

All table attributes. includes primary_key and belongs_tos THINK THIS IS DEPRECATED



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/effective/resources/attributes.rb', line 90

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