Module: Adminpanel::Base::ClassMethods
- Defined in:
- app/models/concerns/adminpanel/base.rb
Overview
static(class) methods
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
API methods.
-
#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.
15 16 17 |
# File 'app/models/concerns/adminpanel/base.rb', line 15 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.
38 39 40 |
# File 'app/models/concerns/adminpanel/base.rb', line 38 def collection_name display_name.pluralize(I18n.default_locale) end |
#collection_routes ⇒ Object
147 148 149 |
# File 'app/models/concerns/adminpanel/base.rb', line 147 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)
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'app/models/concerns/adminpanel/base.rb', line 56 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
27 28 29 |
# File 'app/models/concerns/adminpanel/base.rb', line 27 def display_name 'please overwrite self.display_name' end |
#fb_share? ⇒ Boolean
135 136 137 |
# File 'app/models/concerns/adminpanel/base.rb', line 135 def fb_share? false end |
#form_attributes ⇒ Object
The fields and the types that should be used to generate form and display fields
21 22 23 |
# File 'app/models/concerns/adminpanel/base.rb', line 21 def form_attributes [] end |
#gallery_class ⇒ Object
gets the class gallery and return it’s class
102 103 104 |
# File 'app/models/concerns/adminpanel/base.rb', line 102 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
90 91 92 93 94 95 96 97 98 99 |
# File 'app/models/concerns/adminpanel/base.rb', line 90 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
42 43 44 45 46 47 48 49 50 51 |
# File 'app/models/concerns/adminpanel/base.rb', line 42 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
77 78 79 80 81 82 83 84 85 86 |
# File 'app/models/concerns/adminpanel/base.rb', line 77 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
127 128 129 130 131 132 133 |
# File 'app/models/concerns/adminpanel/base.rb', line 127 def has_route?(route) if (!exclude?(route)) && include_route(route) true else false end end |
#has_sortable_gallery? ⇒ Boolean
155 156 157 158 159 |
# File 'app/models/concerns/adminpanel/base.rb', line 155 def has_sortable_gallery? if has_gallery? gallery_class.is_sortable? end end |
#icon ⇒ Object
side menu icon
32 33 34 |
# File 'app/models/concerns/adminpanel/base.rb', line 32 def icon 'truck' end |
#is_sortable? ⇒ Boolean
151 152 153 |
# File 'app/models/concerns/adminpanel/base.rb', line 151 def is_sortable? false end |
#member_routes ⇒ Object
143 144 145 |
# File 'app/models/concerns/adminpanel/base.rb', line 143 def member_routes [] end |
#mount_images(relation) ⇒ Object
API methods
9 10 11 12 |
# File 'app/models/concerns/adminpanel/base.rb', line 9 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]
111 112 113 114 115 116 117 118 119 120 121 |
# File 'app/models/concerns/adminpanel/base.rb', line 111 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
123 124 125 |
# File 'app/models/concerns/adminpanel/base.rb', line 123 def { path: collection_name.parameterize } end |
#twitter_share? ⇒ Boolean
139 140 141 |
# File 'app/models/concerns/adminpanel/base.rb', line 139 def twitter_share? false end |