Module: Adminpanel::Base::ClassMethods
- Defined in:
- app/models/concerns/adminpanel/base.rb
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
-
#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.
-
#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
return true if model has adminpanel_file_field in it’s attributes.
- #has_route?(route) ⇒ Boolean
- #has_sortable_gallery? ⇒ Boolean
-
#icon ⇒ Object
side menu icon.
- #is_sortable? ⇒ Boolean
- #member_routes ⇒ Object
-
#mount_images(relation) ⇒ Object
Adminpanel API.
-
#relationships_of(type_property) ⇒ Object
returns all the class of the attributes of a given type.
- #routes_options ⇒ Object
- #twitter_share? ⇒ Boolean
Instance Method Details
#belongs_to(name, scope = nil, options = {}) ⇒ Object
implementing cache by default.
14 15 16 |
# File 'app/models/concerns/adminpanel/base.rb', line 14 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.
37 38 39 |
# File 'app/models/concerns/adminpanel/base.rb', line 37 def collection_name display_name.pluralize(I18n.default_locale) end |
#collection_routes ⇒ Object
146 147 148 |
# File 'app/models/concerns/adminpanel/base.rb', line 146 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)
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'app/models/concerns/adminpanel/base.rb', line 55 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'] != 'adminpanel_file_field' #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
26 27 28 |
# File 'app/models/concerns/adminpanel/base.rb', line 26 def display_name 'please overwrite self.display_name' end |
#fb_share? ⇒ Boolean
134 135 136 |
# File 'app/models/concerns/adminpanel/base.rb', line 134 def fb_share? false end |
#form_attributes ⇒ Object
The fields and the types that should be used to generate form and display fields
20 21 22 |
# File 'app/models/concerns/adminpanel/base.rb', line 20 def form_attributes [] end |
#gallery_class ⇒ Object
gets the class gallery and return it’s class
101 102 103 |
# File 'app/models/concerns/adminpanel/base.rb', line 101 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
89 90 91 92 93 94 95 96 97 98 |
# File 'app/models/concerns/adminpanel/base.rb', line 89 def gallery_relationship form_attributes.each do |fields| fields.each do |attribute, properties| if properties['type'] == 'adminpanel_file_field' return attribute end end end return false end |
#get_attribute_label(field) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'app/models/concerns/adminpanel/base.rb', line 41 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
76 77 78 79 80 81 82 83 84 85 |
# File 'app/models/concerns/adminpanel/base.rb', line 76 def has_gallery? form_attributes.each do |fields| fields.each do |attribute, properties| if properties['type'] == 'adminpanel_file_field' return true end end end return false end |
#has_route?(route) ⇒ Boolean
126 127 128 129 130 131 132 |
# File 'app/models/concerns/adminpanel/base.rb', line 126 def has_route?(route) if (!exclude?(route)) && include_route(route) true else false end end |
#has_sortable_gallery? ⇒ Boolean
154 155 156 157 158 |
# File 'app/models/concerns/adminpanel/base.rb', line 154 def has_sortable_gallery? if has_gallery? gallery_class.is_sortable? end end |
#icon ⇒ Object
side menu icon
31 32 33 |
# File 'app/models/concerns/adminpanel/base.rb', line 31 def icon 'truck' end |
#is_sortable? ⇒ Boolean
150 151 152 |
# File 'app/models/concerns/adminpanel/base.rb', line 150 def is_sortable? false end |
#member_routes ⇒ Object
142 143 144 |
# File 'app/models/concerns/adminpanel/base.rb', line 142 def member_routes [] end |
#mount_images(relation) ⇒ Object
Adminpanel API
8 9 10 11 |
# File 'app/models/concerns/adminpanel/base.rb', line 8 def mount_images(relation) has_many relation, dependent: :destroy accepts_nested_attributes_for relation, allow_destroy: true 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]
110 111 112 113 114 115 116 117 118 119 120 |
# File 'app/models/concerns/adminpanel/base.rb', line 110 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
122 123 124 |
# File 'app/models/concerns/adminpanel/base.rb', line 122 def { path: collection_name.parameterize } end |
#twitter_share? ⇒ Boolean
138 139 140 |
# File 'app/models/concerns/adminpanel/base.rb', line 138 def twitter_share? false end |