Class: Carnival::BaseAdminPresenter
- Inherits:
-
Object
- Object
- Carnival::BaseAdminPresenter
- Includes:
- Dsl
- Defined in:
- app/presenters/carnival/base_admin_presenter.rb
Instance Method Summary collapse
- #actions ⇒ Object
- #actions_for_page ⇒ Object
- #actions_for_record ⇒ Object
- #advanced_search_fields ⇒ Object
- #batch_actions ⇒ Object
- #build_relation_field(field, model_object) ⇒ Object
- #controller_class_name ⇒ Object
- #controller_name ⇒ Object
- #controller_to_field(field) ⇒ Object
- #date_filter_field ⇒ Object
- #default_scope ⇒ Object
- #default_sort_direction ⇒ Object
- #default_sortable_field ⇒ Object
- #field_type(field) ⇒ Object
- #fields ⇒ Object
- #fields_for_action(action) ⇒ Object
- #form_for(action) ⇒ Object
- #full_model_name ⇒ Object
- #get_association(association) ⇒ Object
- #get_association_from_field(field) ⇒ Object
- #get_class_for(field) ⇒ Object
- #get_field(field) ⇒ Object
- #get_related_class(relation_name) ⇒ Object
- #get_related_class_for_field(field_name) ⇒ Object
- #has_action?(action) ⇒ Boolean
- #has_batch_actions? ⇒ Boolean
- #index_as ⇒ Object
-
#initialize ⇒ BaseAdminPresenter
constructor
A new instance of BaseAdminPresenter.
- #is_from_carnival? ⇒ Boolean
- #is_one_to_one_relation?(field_name) ⇒ Boolean
- #is_relation_belongs_to?(field) ⇒ Boolean
- #is_relation_has_many?(field) ⇒ Boolean
- #items_per_page ⇒ Object
- #join_tables ⇒ Object
- #load_dependent_select_options_path ⇒ Object
- #model_class ⇒ Object
- #model_name ⇒ Object
- #model_params(params) ⇒ Object
- #model_path(action, extra_params = {}) ⇒ Object
- #must_render_field?(nested_in, field, model_object) ⇒ Boolean
- #parse_advanced_search(records, search_syntax) ⇒ Object
- #presenter_name ⇒ Object
- #related_presenter(field, _not_used = nil) ⇒ Object (also: #presenter_to_field)
- #relation_field?(field_name) ⇒ Boolean
- #relation_model(field) ⇒ Object
- #relation_path(field, record) ⇒ Object
- #relation_type(sym) ⇒ Object
- #render_action?(record, record_action, action = nil) ⇒ Boolean
- #render_field(field_name, record) ⇒ Object
- #render_field?(field, action = nil) ⇒ Boolean
- #renderer_for(field_name) ⇒ Object
- #scopes ⇒ Object
- #table_name ⇒ Object
- #translate_field(field_name) ⇒ Object
- #validates ⇒ Object
Constructor Details
#initialize ⇒ BaseAdminPresenter
Returns a new instance of BaseAdminPresenter.
7 8 9 10 11 12 13 14 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 7 def initialize return unless model_class.present? @klass_service = KlassService.new model_class @advanced_search_parser = Presenters::AdvancedSearchParser.new(@klass_service) @validators = [Carnival::PresenterValidators::FieldValidator] validates end |
Instance Method Details
#actions ⇒ Object
52 53 54 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 52 def actions @@actions[presenter_class_name] end |
#actions_for_page ⇒ Object
64 65 66 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 64 def actions_for_page filter_actions([:new], :page) end |
#actions_for_record ⇒ Object
60 61 62 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 60 def actions_for_record filter_actions([:show, :edit, :destroy], :record) end |
#advanced_search_fields ⇒ Object
213 214 215 216 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 213 def advanced_search_fields fields = @@fields[presenter_class_name] @advanced_search_parser.get_advanced_search_fields fields end |
#batch_actions ⇒ Object
56 57 58 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 56 def batch_actions @@batch_actions[presenter_class_name] end |
#build_relation_field(field, model_object) ⇒ Object
178 179 180 181 182 183 184 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 178 def build_relation_field(field, model_object) if is_relation_belongs_to?(field.name) model_object.send("#{field.name}_build") else model_object.send(field.name).build end end |
#controller_class_name ⇒ Object
148 149 150 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 148 def controller_class_name "#{controller_name}_controller".classify end |
#controller_name ⇒ Object
152 153 154 155 156 157 158 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 152 def controller_name if is_namespaced? "#{extract_namespace}/#{controller_resource_name}".underscore else controller_resource_name end end |
#controller_to_field(field) ⇒ Object
330 331 332 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 330 def controller_to_field(field) "#{extract_namespace}::#{field.name.to_s.classify.pluralize}Controller".constantize.new end |
#date_filter_field ⇒ Object
218 219 220 221 222 223 224 225 226 227 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 218 def date_filter_field date_field = nil @@fields[presenter_class_name].each do |key, field| if field.date_filter? date_field = field break end end date_field end |
#default_scope ⇒ Object
198 199 200 201 202 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 198 def default_scope return if scopes.empty? @@scopes[presenter_class_name].values.find(&:default?) || @@scopes[presenter_class_name].values.first end |
#default_sort_direction ⇒ Object
209 210 211 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 209 def default_sort_direction default_sortable_field.default_sort_direction end |
#default_sortable_field ⇒ Object
204 205 206 207 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 204 def default_sortable_field @@fields[presenter_class_name].values.find(&:default_sortable?) || @@fields[presenter_class_name].values.first end |
#field_type(field) ⇒ Object
260 261 262 263 264 265 266 267 268 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 260 def field_type(field) type = model_class.columns_hash[field.to_s].try(:type) if relation_field?(field.to_sym) then :relation elsif type == :date || type == :datetime then type elsif type == :number || type == :float then :number elsif type == :integer and model_class.const_defined? field.upcase and field != :id then :enum else type end end |
#fields ⇒ Object
76 77 78 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 76 def fields @@fields[presenter_class_name] end |
#fields_for_action(action) ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 88 def fields_for_action(action) fields_action = {} @@fields[presenter_class_name].each do |key, field| if field.valid_for_action?(action) fields_action[key] = field end end fields_action end |
#form_for(action) ⇒ Object
98 99 100 101 102 103 104 105 106 107 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 98 def form_for(action) if @@forms[presenter_class_name].present? && @@forms[presenter_class_name][action].present? form = @@forms[presenter_class_name][action] else form = Carnival::Form.new(action) end form.fields = fields_for_action(action) form end |
#full_model_name ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 136 def full_model_name if Carnival::Config.use_full_model_name == false and !is_from_carnival? return model_name end if @@model_names[presenter_class_name].nil? self.class.to_s.gsub("Presenter", "").underscore else @@model_names[presenter_class_name] end end |
#get_association(association) ⇒ Object
242 243 244 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 242 def get_association(association) @klass_service.get_association(association) end |
#get_association_from_field(field) ⇒ Object
303 304 305 306 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 303 def get_association_from_field(field) field = get_field(field) field.specified_association? ? field.association_name : field.name end |
#get_class_for(field) ⇒ Object
312 313 314 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 312 def get_class_for field @klass_service. field end |
#get_field(field) ⇒ Object
80 81 82 83 84 85 86 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 80 def get_field(field) if field.respond_to? :to_sym @@fields[presenter_class_name][field.to_sym] else field end end |
#get_related_class(relation_name) ⇒ Object
308 309 310 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 308 def relation_name @klass_service. relation_name end |
#get_related_class_for_field(field_name) ⇒ Object
299 300 301 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 299 def (field_name) get_association_from_field(field_name) end |
#has_action?(action) ⇒ Boolean
68 69 70 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 68 def has_action?(action) @@actions[presenter_class_name].present? && @@actions[presenter_class_name][action].present? end |
#has_batch_actions? ⇒ Boolean
26 27 28 29 30 31 32 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 26 def has_batch_actions? if @@batch_actions[presenter_class_name].present? @@batch_actions[presenter_class_name].keys.size > 0 else false end end |
#index_as ⇒ Object
48 49 50 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 48 def index_as @@index_as[presenter_class_name] ||= :table end |
#is_from_carnival? ⇒ Boolean
132 133 134 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 132 def is_from_carnival? self.class.to_s.include? 'Carnival::' end |
#is_one_to_one_relation?(field_name) ⇒ Boolean
275 276 277 278 279 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 275 def is_one_to_one_relation?(field_name) field = get_field(field_name) @klass_service.is_a_belongs_to_relation?(field.association_name) || @klass_service.is_a_has_one_relation?(field.association_name) end |
#is_relation_belongs_to?(field) ⇒ Boolean
250 251 252 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 250 def is_relation_belongs_to?(field) model_class.reflect_on_association(field.to_sym).macro == :belongs_to end |
#is_relation_has_many?(field) ⇒ Boolean
254 255 256 257 258 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 254 def is_relation_has_many?(field) field = get_field(field) association = @klass_service.relation_type(field.association_name || field.name) association.to_s.include? 'many' end |
#items_per_page ⇒ Object
34 35 36 37 38 39 40 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 34 def items_per_page @@items_per_page[presenter_class_name] ||= {} unless @@items_per_page[presenter_class_name][:items_per_page].present? and @@items_per_page[presenter_class_name][:items_per_page] > 0 @@items_per_page[presenter_class_name][:items_per_page] = Carnival::Config.items_per_page end @@items_per_page[presenter_class_name][:items_per_page] end |
#join_tables ⇒ Object
164 165 166 167 168 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 164 def join_tables @@fields[presenter_class_name].map do |key, field| field.association_name if relation_field? key.to_sym end end |
#load_dependent_select_options_path ⇒ Object
334 335 336 337 338 339 340 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 334 def if extract_namespace.present? "/#{extract_namespace.downcase}/carnival-base/load_dependent_select_options" else '/carnival-base/load_dependent_select_options' end end |
#model_class ⇒ Object
229 230 231 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 229 def model_class full_model_name.classify.constantize rescue nil end |
#model_name ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 109 def model_name if @@model_names[presenter_class_name].nil? self.class.to_s.split('::').last.gsub('Presenter', '').underscore else if @@model_names[presenter_class_name].include?('/') @@model_names[presenter_class_name].split('/').last else @@model_names[presenter_class_name] end end end |
#model_params(params) ⇒ Object
121 122 123 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 121 def model_params(params) params.select { |key, _| key != 'action' && key != 'controller' } end |
#model_path(action, extra_params = {}) ⇒ Object
125 126 127 128 129 130 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 125 def model_path(action, extra_params = {}) params = extra_params.reverse_merge( controller: controller_name, action: action ).merge(only_path: true) url_for(params) end |
#must_render_field?(nested_in, field, model_object) ⇒ Boolean
186 187 188 189 190 191 192 193 194 195 196 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 186 def must_render_field?(nested_in, field, model_object) return true unless nested_in.present? if field.as == :partial true elsif nested_in.class == model_object.send(field.name).class || nested_in.class.name.underscore.split('/').last == field.name false else true end end |
#parse_advanced_search(records, search_syntax) ⇒ Object
316 317 318 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 316 def parse_advanced_search records, search_syntax @advanced_search_parser.parse_advanced_search @@fields[presenter_class_name], records, search_syntax end |
#presenter_name ⇒ Object
22 23 24 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 22 def presenter_name self.class.to_s end |
#related_presenter(field, _not_used = nil) ⇒ Object Also known as: presenter_to_field
320 321 322 323 324 325 326 327 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 320 def (field, _not_used = nil) field = get_field(field) if field.presenter_class.present? field.presenter_class.constantize.new else infer_presenter(field) end end |
#relation_field?(field_name) ⇒ Boolean
233 234 235 236 237 238 239 240 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 233 def relation_field?(field_name) field = get_field(field_name) if field.present? field.specified_association? or @klass_service.relation?(field.name) else false end end |
#relation_model(field) ⇒ Object
270 271 272 273 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 270 def relation_model(field) field = get_field(field) @klass_service.get_association(field.association_name).klass end |
#relation_path(field, record) ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 281 def relation_path(field, record) field = get_field(field) return nil if !relation_field?(field) relation_name = (field) = record.send(field.association_name) if .present? if is_one_to_one_relation?(field) params = { controller: "#{extract_namespace.downcase}/#{relation_name}", action: :show, id: .id } else params = { controller: "#{extract_namespace.downcase}/#{get_association_from_field(field)}", action: :index, advanced_search: (field.name, record) } end params = params.merge(only_path: true) generate_route_path params else '#' end end |
#relation_type(sym) ⇒ Object
246 247 248 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 246 def relation_type sym @klass_service.relation_type sym end |
#render_action?(record, record_action, action = nil) ⇒ Boolean
174 175 176 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 174 def render_action?(record, record_action, action = nil) true end |
#render_field(field_name, record) ⇒ Object
342 343 344 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 342 def render_field(field_name, record) renderer_for(field_name).render_field(record) end |
#render_field?(field, action = nil) ⇒ Boolean
170 171 172 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 170 def render_field?(field, action = nil) true end |
#renderer_for(field_name) ⇒ Object
346 347 348 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 346 def renderer_for(field_name) FieldRenderers::RendererCreator.create_field_renderer(self, field_name) end |
#scopes ⇒ Object
72 73 74 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 72 def scopes @@scopes[presenter_class_name] || {} end |
#table_name ⇒ Object
160 161 162 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 160 def table_name model_class.table_name end |
#translate_field(field_name) ⇒ Object
350 351 352 353 354 355 356 357 358 359 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 350 def translate_field field_name field = get_field(field_name) if field.specified_association? = model_class.reflect_on_association(field.association_name.to_sym).klass .human_attribute_name(field.association_field_name) else field_key = field.name_for_translation model_class.human_attribute_name field_key end end |
#validates ⇒ Object
16 17 18 19 20 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 16 def validates @validators.each do |validator| validator.new(self).validates end end |