Module: Adminpanel::Base::ClassMethods

Defined in:
app/models/concerns/adminpanel/base.rb

Constant Summary collapse

FILE_FIELD_NAME =
'adminpanel_file_field'

Instance Method Summary collapse

Instance Method Details

#belongs_to(name, scope = nil, options = {}) ⇒ Object

implementing cache by default.



18
19
20
# File 'app/models/concerns/adminpanel/base.rb', line 18

def belongs_to(name, scope = nil, options = {})
  super(name, scope, options.reverse_merge!({touch: true}))
end

#collection_nameObject

The word that is going to be shown in the side menu, routes and breadcrumb.



41
42
43
# File 'app/models/concerns/adminpanel/base.rb', line 41

def collection_name
  display_name.pluralize(I18n.default_locale)
end

#collection_routesObject



177
178
179
# File 'app/models/concerns/adminpanel/base.rb', line 177

def collection_routes
  []
end

#display_attributes(type) ⇒ Object

returns the attributes that should be shown in the correspondin view (some attributes may be filtered from the index table, from the show or even both)



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/concerns/adminpanel/base.rb', line 59

def display_attributes(type)
  display_attributes = []
  form_attributes.each do |attribute|
    attribute.each do |name, properties|
      if (
        properties['show'].nil? ||
        properties['show'] == 'true' ||
        (
          properties['show'] == type &&
          properties['type'] != FILE_FIELD_NAME #file fields get only displayed in form
        )
      )
        display_attributes << attribute
      end
    end
  end
  return display_attributes
end

#display_nameObject

The name that is going to be shown in the new button and that is going to be pluralized (if not overwritten) to generate collection_name



30
31
32
# File 'app/models/concerns/adminpanel/base.rb', line 30

def display_name
  'please overwrite self.display_name'
end

#fb_share?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'app/models/concerns/adminpanel/base.rb', line 165

def fb_share?
  false
end

#form_attributesObject

The fields and the types that should be used to generate form and display fields



24
25
26
# File 'app/models/concerns/adminpanel/base.rb', line 24

def form_attributes
  []
end

#galleriesObject

Returns an array of all the adminpanel_field_field fields found in form_attributes



92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/models/concerns/adminpanel/base.rb', line 92

def galleries
  galleries = {}
  form_attributes.each do |fields|
    fields.each do |attribute, properties|
      if properties['type'] == FILE_FIELD_NAME
        galleries["#{attribute.singularize}"] = "adminpanel/#{attribute}".classify.constantize.to_s
      end
    end
  end

  return galleries
end

gets the class gallery and return it’s class



132
133
134
# File 'app/models/concerns/adminpanel/base.rb', line 132

def gallery_class
  "adminpanel/#{gallery_relationship}".classify.constantize
end

returns the attribute that should be namespaced to be the class ex: returns ‘productfiles’, so class is Adminpanel::Productfile



120
121
122
123
124
125
126
127
128
129
# File 'app/models/concerns/adminpanel/base.rb', line 120

def gallery_relationship
  form_attributes.each do |fields|
    fields.each do |attribute, properties|
      if properties['type'] == FILE_FIELD_NAME
        return attribute
      end
    end
  end
  return false
end

#get_attribute_label(field) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'app/models/concerns/adminpanel/base.rb', line 45

def get_attribute_label(field)
  form_attributes.each do |attribute|
    attribute.each do |name, properties|
      if name == field
        return properties['label']
      end
    end
  end
  return "field #{field} 'label' property not found :("
end

#has_gallery?Boolean

return true if model has adminpanel_file_field in it’s attributes

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
89
# File 'app/models/concerns/adminpanel/base.rb', line 80

def has_gallery?
  form_attributes.each do |fields|
    fields.each do |attribute, properties|
      if properties['type'] == FILE_FIELD_NAME
        return true
      end
    end
  end
  return false
end

#has_route?(route) ⇒ Boolean

Returns:

  • (Boolean)


157
158
159
160
161
162
163
# File 'app/models/concerns/adminpanel/base.rb', line 157

def has_route?(route)
  if (!exclude?(route)) && include_route(route)
    true
  else
    false
  end
end

#has_sortable_gallery?Boolean

Returns:

  • (Boolean)


185
186
187
# File 'app/models/concerns/adminpanel/base.rb', line 185

def has_sortable_gallery?
  !sortable_galleries.empty?
end

#iconObject

side menu icon



35
36
37
# File 'app/models/concerns/adminpanel/base.rb', line 35

def icon
  'truck'
end

#is_sortable?Boolean

Returns:

  • (Boolean)


181
182
183
# File 'app/models/concerns/adminpanel/base.rb', line 181

def is_sortable?
  false
end

#member_routesObject



173
174
175
# File 'app/models/concerns/adminpanel/base.rb', line 173

def member_routes
  []
end

#mount_images(relation) ⇒ Object

Adminpanel API



10
11
12
13
14
15
# File 'app/models/concerns/adminpanel/base.rb', line 10

def mount_images(relation)
  has_many relation, dependent: :destroy, as: :model
  accepts_nested_attributes_for relation, allow_destroy: true
  after_save :destroy_unattached_images
  after_save :correlative_order_gallery, if: Proc.new { |model| model.class.has_sortable_gallery? }
end

#relationships_of(type_property) ⇒ Object

returns all the class of the attributes of a given type. Usage: To get all classes of all belongs_to attributes:

@model.relationships_of('belongs_to')
# => ['Adminpanel::Category', Adminpanel::ModelBelongTo]


141
142
143
144
145
146
147
148
149
150
151
# File 'app/models/concerns/adminpanel/base.rb', line 141

def relationships_of(type_property)
  classes_of_relation = []
  form_attributes.each do |fields|
    fields.each do |attribute, properties|
      if properties['type'] == type_property
        classes_of_relation << properties['model'].classify.constantize
      end
    end
  end
  return classes_of_relation
end

#routes_optionsObject



153
154
155
# File 'app/models/concerns/adminpanel/base.rb', line 153

def routes_options
  { path: collection_name.parameterize }
end

#sortable_galleriesObject



105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/models/concerns/adminpanel/base.rb', line 105

def sortable_galleries
  galleries = {}
  form_attributes.each do |fields|
    fields.each do |attribute, properties|
      if properties['type'] == FILE_FIELD_NAME && "adminpanel/#{attribute}".classify.constantize.is_sortable?
        galleries["#{attribute.singularize}"] = "adminpanel/#{attribute}".classify.constantize.to_s
      end
    end
  end

  galleries
end

#to_controller_nameObject



189
190
191
# File 'app/models/concerns/adminpanel/base.rb', line 189

def to_controller_name
  to_s.demodulize.underscore.pluralize
end

#twitter_share?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'app/models/concerns/adminpanel/base.rb', line 169

def twitter_share?
  false
end