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
- #controller_to_field_sym(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(params) ⇒ 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 = nil) ⇒ Object
- #must_render_field?(nested_in, field, model_object) ⇒ Boolean
- #parse_advanced_search(records, search_syntax) ⇒ Object
- #presenter_name ⇒ Object
- #presenter_to_field(field, record) ⇒ Object
- #presenter_to_field_sym(field) ⇒ Object
- #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, action = nil) ⇒ Boolean
- #scopes ⇒ Object
- #table_name ⇒ Object
- #validates ⇒ Object
Constructor Details
#initialize(params) ⇒ BaseAdminPresenter
Returns a new instance of BaseAdminPresenter.
7 8 9 10 11 12 13 14 15 16 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 7 def initialize(params) @controller = params[:controller] if model_class.present? @klass_service = KlassService.new model_class @advanced_search_parser = Presenters::AdvancedSearchParser.new(@klass_service) @validators = [Carnival::PresenterValidators::FieldValidator] validates end end |
Instance Method Details
#actions ⇒ Object
54 55 56 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 54 def actions @@actions[presenter_class_name] end |
#actions_for_page ⇒ Object
66 67 68 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 66 def actions_for_page filter_actions([:new], :page) end |
#actions_for_record ⇒ Object
62 63 64 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 62 def actions_for_record filter_actions([:show, :edit, :destroy], :record) end |
#advanced_search_fields ⇒ Object
216 217 218 219 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 216 def advanced_search_fields fields = @@fields[presenter_class_name] @advanced_search_parser.get_advanced_search_fields fields end |
#batch_actions ⇒ Object
58 59 60 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 58 def batch_actions @@batch_actions[presenter_class_name] end |
#build_relation_field(field, model_object) ⇒ Object
181 182 183 184 185 186 187 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 181 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
150 151 152 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 150 def controller_class_name @controller.class.name end |
#controller_name ⇒ Object
154 155 156 157 158 159 160 161 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 154 def controller_name namespace = extract_namespace if namespace.present? "#{extract_namespace.downcase}/#{@controller.controller_name}" else @controller.controller_name end end |
#controller_to_field(field) ⇒ Object
331 332 333 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 331 def controller_to_field field "#{extract_namespace}::#{field.name.to_s.classify.pluralize}Controller".constantize.send("new") end |
#controller_to_field_sym(field) ⇒ Object
335 336 337 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 335 def controller_to_field_sym field "#{extract_namespace}::#{field.to_s.classify.pluralize}Controller".constantize.send("new") end |
#date_filter_field ⇒ Object
221 222 223 224 225 226 227 228 229 230 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 221 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
201 202 203 204 205 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 201 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
212 213 214 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 212 def default_sort_direction default_sortable_field.default_sort_direction end |
#default_sortable_field ⇒ Object
207 208 209 210 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 207 def default_sortable_field @@fields[presenter_class_name].values.find(&:default_sortable?) || @@fields[presenter_class_name].values.first end |
#field_type(field) ⇒ Object
263 264 265 266 267 268 269 270 271 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 263 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
78 79 80 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 78 def fields @@fields[presenter_class_name] end |
#fields_for_action(action) ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 90 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
100 101 102 103 104 105 106 107 108 109 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 100 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
138 139 140 141 142 143 144 145 146 147 148 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 138 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
245 246 247 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 245 def get_association(association) @klass_service.get_association(association) end |
#get_association_from_field(field) ⇒ Object
306 307 308 309 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 306 def get_association_from_field(field) field = get_field(field) field.specified_association? ? field.association_name : field.name end |
#get_class_for(field) ⇒ Object
315 316 317 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 315 def get_class_for field @klass_service. field end |
#get_field(field) ⇒ Object
82 83 84 85 86 87 88 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 82 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
311 312 313 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 311 def relation_name @klass_service. relation_name end |
#get_related_class_for_field(field_name) ⇒ Object
302 303 304 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 302 def (field_name) get_association_from_field(field_name) end |
#has_action?(action) ⇒ Boolean
70 71 72 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 70 def has_action?(action) @@actions[presenter_class_name].present? && @@actions[presenter_class_name][action].present? end |
#has_batch_actions? ⇒ Boolean
28 29 30 31 32 33 34 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 28 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
50 51 52 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 50 def index_as @@index_as[presenter_class_name] ||= :table end |
#is_from_carnival? ⇒ Boolean
134 135 136 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 134 def is_from_carnival? self.class.to_s.include? 'Carnival::' end |
#is_one_to_one_relation?(field_name) ⇒ Boolean
278 279 280 281 282 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 278 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
253 254 255 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 253 def is_relation_belongs_to?(field) model_class.reflect_on_association(field.to_sym).macro == :belongs_to end |
#is_relation_has_many?(field) ⇒ Boolean
257 258 259 260 261 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 257 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
36 37 38 39 40 41 42 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 36 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
167 168 169 170 171 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 167 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
339 340 341 342 343 344 345 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 339 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
232 233 234 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 232 def model_class full_model_name.classify.constantize rescue nil end |
#model_name ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 111 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
123 124 125 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 123 def model_params(params) params.select{|key, value| key != "action" && key != "controller"} end |
#model_path(action, extra_params = nil) ⇒ Object
127 128 129 130 131 132 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 127 def model_path(action, extra_params=nil) params = {controller: controller_name, action: action} params = params.merge(extra_params) if extra_params.present? params = params.merge(:only_path => true) url_for(params) end |
#must_render_field?(nested_in, field, model_object) ⇒ Boolean
189 190 191 192 193 194 195 196 197 198 199 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 189 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
319 320 321 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 319 def parse_advanced_search records, search_syntax @advanced_search_parser.parse_advanced_search @@fields[presenter_class_name], records, search_syntax end |
#presenter_name ⇒ Object
24 25 26 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 24 def presenter_name self.class.to_s end |
#presenter_to_field(field, record) ⇒ Object
323 324 325 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 323 def presenter_to_field field, record "#{extract_namespace}::#{field.name.to_s.singularize.classify}Presenter".constantize.send("new", :controller => controller_to_field(field)) end |
#presenter_to_field_sym(field) ⇒ Object
327 328 329 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 327 def presenter_to_field_sym field "#{extract_namespace}::#{field.to_s.singularize.classify}Presenter".constantize.send("new", :controller => controller_to_field_sym(field)) end |
#relation_field?(field_name) ⇒ Boolean
236 237 238 239 240 241 242 243 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 236 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
273 274 275 276 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 273 def relation_model(field) field = get_field(field) @klass_service.get_association(field.association_name).klass end |
#relation_path(field, record) ⇒ Object
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 284 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
249 250 251 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 249 def relation_type sym @klass_service.relation_type sym end |
#render_action?(record, record_action, action = nil) ⇒ Boolean
177 178 179 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 177 def render_action?(record, record_action, action = nil) true end |
#render_field?(field, action = nil) ⇒ Boolean
173 174 175 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 173 def render_field?(field, action = nil) true end |
#scopes ⇒ Object
74 75 76 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 74 def scopes @@scopes[presenter_class_name] || {} end |
#table_name ⇒ Object
163 164 165 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 163 def table_name model_class.table_name end |
#validates ⇒ Object
18 19 20 21 22 |
# File 'app/presenters/carnival/base_admin_presenter.rb', line 18 def validates @validators.each do |validator| validator.new(self).validates end end |