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
-
#belongs_to(name, scope = nil, **options) ⇒ Object
implementing cache by default.
-
#collection_name ⇒ Object
The word that is going to be shown in the side menu, routes and breadcrumb.
-
#collection_routes ⇒ Object
Additional collection routes for this resource.
-
#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).
-
#display_name ⇒ Object
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.
- #fb_share? ⇒ Boolean
-
#form_attributes ⇒ Object
The fields and the types that should be used to generate form and display fields.
- #form_url ⇒ Object
-
#galleries ⇒ Object
Returns an array with all the adminpanel_file_field`s attributes found in form_attributes => { sectionfiles: Adminpanel::SectionFile, … }.
-
#gallery_class ⇒ Object
gets the class gallery and return it’s class.
-
#gallery_relationship ⇒ Object
returns the attribute that should be namespaced to be the class ex: returns ‘productfiles’, so class is Adminpanel::Productfile.
- #get_attribute_label(field) ⇒ Object
-
#has_gallery? ⇒ Boolean
Check if this models has a gallery in its attributes.
- #has_route?(route) ⇒ Boolean
- #has_sortable_gallery? ⇒ Boolean
-
#has_trix_gallery? ⇒ Boolean
Check if this models has a trix editor with a gallery in its attributes.
-
#icon ⇒ Object
fontawesome icon to be used in the side-menu.
- #is_sortable? ⇒ Boolean
-
#member_routes ⇒ Object
Additional member routes for this resource.
- #mount_images(relation) ⇒ Object
-
#relationships_of(type_property) ⇒ Object
Search for a model attribute’s that are of a given type Usage: To get all classes of all belongs_to attributes: @model.relationships_of(‘belongs_to’) # => [‘Adminpanel::Category’, Adminpanel::ModelBelongTo].
-
#routes_options ⇒ Object
routes options to be used when generating this model routes.
-
#sortable_galleries ⇒ Object
Returns an array with all the adminpanel_file_field`s attributes who are sortable found in form_attributes.
- #to_controller_name ⇒ Object
Instance Method Details
#belongs_to(name, scope = nil, **options) ⇒ Object
implementing cache by default.
20 21 22 |
# File 'app/models/concerns/adminpanel/base.rb', line 20 def belongs_to(name, scope = nil, **) super(name, scope, .reverse_merge!({touch: true})) end |
#collection_name ⇒ Object
The word that is going to be shown in the side menu, routes and breadcrumb.
43 44 45 |
# File 'app/models/concerns/adminpanel/base.rb', line 43 def collection_name display_name.pluralize(I18n.default_locale) end |
#collection_routes ⇒ Object
Additional collection routes for this resource
199 200 201 |
# File 'app/models/concerns/adminpanel/base.rb', line 199 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)
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/models/concerns/adminpanel/base.rb', line 61 def display_attributes(type) display_attributes = [] form_attributes.each do |attribute| attribute.each do |_, 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_name ⇒ Object
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
32 33 34 |
# File 'app/models/concerns/adminpanel/base.rb', line 32 def display_name 'please overwrite self.display_name' end |
#fb_share? ⇒ Boolean
189 190 191 |
# File 'app/models/concerns/adminpanel/base.rb', line 189 def fb_share? false end |
#form_attributes ⇒ Object
The fields and the types that should be used to generate form and display fields
26 27 28 |
# File 'app/models/concerns/adminpanel/base.rb', line 26 def form_attributes [] end |
#form_url ⇒ Object
215 216 217 |
# File 'app/models/concerns/adminpanel/base.rb', line 215 def form_url self end |
#galleries ⇒ Object
Returns an array with all the adminpanel_file_field`s attributes found in form_attributes
> { sectionfiles: Adminpanel::SectionFile, … }
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'app/models/concerns/adminpanel/base.rb', line 110 def galleries galleries = {} form_attributes.each do |fields| fields.each do |relation, properties| if properties['type'] == FILE_FIELD_NAME galleries["#{relation.singularize}"] = "adminpanel/#{relation}".classify.constantize.to_s end end end return galleries end |
#gallery_class ⇒ Object
gets the class gallery and return it’s class
153 154 155 |
# File 'app/models/concerns/adminpanel/base.rb', line 153 def gallery_class "adminpanel/#{gallery_relationship}".classify.constantize end |
#gallery_relationship ⇒ Object
returns the attribute that should be namespaced to be the class ex: returns ‘productfiles’, so class is Adminpanel::Productfile
141 142 143 144 145 146 147 148 149 150 |
# File 'app/models/concerns/adminpanel/base.rb', line 141 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
47 48 49 50 51 52 53 54 55 56 |
# File 'app/models/concerns/adminpanel/base.rb', line 47 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 end |
#has_gallery? ⇒ Boolean
Check if this models has a gallery in its attributes
82 83 84 85 86 87 88 89 90 91 |
# File 'app/models/concerns/adminpanel/base.rb', line 82 def has_gallery? form_attributes.each do |fields| fields.each do |attribute, properties| if properties['type'] == FILE_FIELD_NAME return true end end end false end |
#has_route?(route) ⇒ Boolean
181 182 183 184 185 186 187 |
# File 'app/models/concerns/adminpanel/base.rb', line 181 def has_route?(route) if (!exclude_route?(route)) && include_route?(route) true else false end end |
#has_sortable_gallery? ⇒ Boolean
207 208 209 |
# File 'app/models/concerns/adminpanel/base.rb', line 207 def has_sortable_gallery? !sortable_galleries.empty? end |
#has_trix_gallery? ⇒ Boolean
Check if this models has a trix editor with a gallery in its attributes
95 96 97 98 99 100 101 102 103 104 |
# File 'app/models/concerns/adminpanel/base.rb', line 95 def has_trix_gallery? form_attributes.each do |fields| fields.each do |attribute, properties| if properties['type'] == 'wysiwyg_field' && properties['uploader'].present? return true end end end false end |
#icon ⇒ Object
fontawesome icon to be used in the side-menu
37 38 39 |
# File 'app/models/concerns/adminpanel/base.rb', line 37 def icon 'truck' end |
#is_sortable? ⇒ Boolean
203 204 205 |
# File 'app/models/concerns/adminpanel/base.rb', line 203 def is_sortable? false end |
#member_routes ⇒ Object
Additional member routes for this resource
194 195 196 |
# File 'app/models/concerns/adminpanel/base.rb', line 194 def member_routes [] end |
#mount_images(relation) ⇒ Object
12 13 14 15 16 17 |
# File 'app/models/concerns/adminpanel/base.rb', line 12 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
Search for a model attribute’s that are of a given type Usage: To get all classes of all belongs_to attributes:
@model.relationships_of('belongs_to')
# => ['Adminpanel::Category', Adminpanel::ModelBelongTo]
163 164 165 166 167 168 169 170 171 172 173 |
# File 'app/models/concerns/adminpanel/base.rb', line 163 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_options ⇒ Object
routes options to be used when generating this model routes
177 178 179 |
# File 'app/models/concerns/adminpanel/base.rb', line 177 def { path: collection_name.parameterize } end |
#sortable_galleries ⇒ Object
Returns an array with all the adminpanel_file_field`s attributes who are sortable found in form_attributes
126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'app/models/concerns/adminpanel/base.rb', line 126 def sortable_galleries galleries = {} form_attributes.each do |fields| fields.each do |relation, properties| if properties['type'] == FILE_FIELD_NAME && "adminpanel/#{relation}".classify.constantize.is_sortable? galleries["#{relation.singularize}"] = "adminpanel/#{relation}".classify.constantize.to_s end end end galleries end |
#to_controller_name ⇒ Object
211 212 213 |
# File 'app/models/concerns/adminpanel/base.rb', line 211 def to_controller_name to_s.demodulize.underscore.pluralize end |