Class: SlashAdmin::ModelsController
- Inherits:
-
BaseController
- Object
- ActionController::Base
- ApplicationController
- BaseController
- SlashAdmin::ModelsController
- Includes:
- Pagy::Backend
- Defined in:
- app/controllers/slash_admin/models_controller.rb
Instance Method Summary collapse
- #after_save_on_create ⇒ Object
- #after_save_on_update ⇒ Object
- #autocomplete_params ⇒ Object
- #before_validate_on_create ⇒ Object
- #before_validate_on_update ⇒ Object
- #create ⇒ Object
- #destroy ⇒ Object
- #edit ⇒ Object
-
#export_csv(options = {}) ⇒ Object
Export CSV.
- #handle_filtered_search ⇒ Object
- #handle_has_one ⇒ Object
- #handle_specific_fields ⇒ Object
- #index ⇒ Object
- #nestable ⇒ Object
- #new ⇒ Object
- #no_title ⇒ Object
- #show ⇒ Object
-
#tooltips ⇒ Object
Add tooltip to th list view & edit/create view { attr: ‘Value’, title: ‘The title of my model’, }.
- #update ⇒ Object
- #update_params(options = {}) ⇒ Object
Methods inherited from ApplicationController
Instance Method Details
#after_save_on_create ⇒ Object
61 62 |
# File 'app/controllers/slash_admin/models_controller.rb', line 61 def after_save_on_create end |
#after_save_on_update ⇒ Object
147 148 |
# File 'app/controllers/slash_admin/models_controller.rb', line 147 def after_save_on_update end |
#autocomplete_params ⇒ Object
327 328 329 330 331 332 333 334 335 |
# File 'app/controllers/slash_admin/models_controller.rb', line 327 def autocomplete_params aut_params = [] helpers.object_label_methods.each do |m| aut_params << m if model.respond_to? m end raise Exception.new("You have to defined autocomplete_params in your admin model controller") if aut_params.blank? aut_params end |
#before_validate_on_create ⇒ Object
58 59 |
# File 'app/controllers/slash_admin/models_controller.rb', line 58 def before_validate_on_create end |
#before_validate_on_update ⇒ Object
144 145 |
# File 'app/controllers/slash_admin/models_controller.rb', line 144 def before_validate_on_update end |
#create ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'app/controllers/slash_admin/models_controller.rb', line 64 def create :new, @model_class handle_has_one @model = @model_class.new(permit_params) before_validate_on_create handle_specific_fields if @model.valid? if @model.save! after_save_on_create respond_to do |format| format.html do flash[:success] = t("slash_admin.controller.create.success", model_name: @model_name) redirect_to(handle_redirect_after_submit) && return end format.js { render(json: {id: @model.id, name: helpers.show_object(@model)}) && return } end end else flash[:error] = t("slash_admin.controller.create.error", model_name: @model_name) end respond_to do |format| format.html { render :new } format.js { render json: {errors: @model.errors.} } end end |
#destroy ⇒ Object
135 136 137 138 139 140 141 142 |
# File 'app/controllers/slash_admin/models_controller.rb', line 135 def destroy @model_class.find(params[:id]).destroy! flash[:success] = t("slash_admin.controller.delete.success", model_name: @model_name) respond_to do |format| format.html { redirect_to main_app.polymorphic_url([:slash_admin, @model_class]) } format.js end end |
#edit ⇒ Object
94 95 96 97 |
# File 'app/controllers/slash_admin/models_controller.rb', line 94 def edit :edit, @model_class @model = @model_class.find(params[:id]) end |
#export_csv(options = {}) ⇒ Object
Export CSV
315 316 317 |
# File 'app/controllers/slash_admin/models_controller.rb', line 315 def export_csv( = {}) @models_export.to_sql end |
#handle_filtered_search ⇒ Object
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'app/controllers/slash_admin/models_controller.rb', line 226 def handle_filtered_search search = if @model_class.respond_to? :translated_attribute_names @model_class.with_translations(I18n.locale).all else @model_class.all end virtual_fields = [] params[:filters].each do |attr, query| unless query.blank? attr_type = helpers.guess_field_type(@model_class, attr) if @model_class.respond_to?(:translated_attribute_names) && @model_class.translated_attribute_names.include?(attr.to_sym) attr = "#{@model_class.name.singularize.underscore}_translations.#{attr}" end # aggregate_reflections ? attr_prefixed = model.table_name + "." + attr case attr_type # TODO : Should be rewritten when "belongs_to" search = search.eager_load(attr.to_s) search = search.where(attr.to_s + "_id IN (" + query.join(",") + ")") # TODO : Should be rewritten when "has_one" search = search.eager_load(attr.to_s) search = search.where(attr.to_s.pluralize + ".id IN (" + query.join(",") + ")") when "string", "text" query.strip! unless query.strip!.nil? attributes = @model_class.new.attributes.keys if !attributes.include?(attr.to_s) && @model_class.method_defined?(attr.to_s) virtual_fields << attr.to_s else begin search = search.where("unaccent(lower(#{attr_prefixed})) LIKE unaccent(lower(:query))", query: "%#{query}%") rescue search = search.where("lower(#{attr_prefixed}) LIKE lower(:query)", query: "%#{query}%") end end when "date", "datetime" if query.is_a?(String) search = search.where("#{attr_prefixed} = :query", query: query) else if query["from"].present? || query["to"].present? if query["from"].to_date != query["to"].to_date if query["from"].present? search = search.where("#{attr_prefixed} >= :query", query: query["from"].to_date) end if query["to"].present? search = search.where("#{attr_prefixed} <= :query", query: query["to"].to_date) end else search = search.where("#{attr_prefixed} = :query", query: query["from"].to_date) end end end when "decimal", "number", "integer" if query.instance_of?(ActionController::Parameters) if query["from"].present? || query["to"].present? search = search.where("#{attr_prefixed} >= :query", query: query["from"]) if query["from"].present? search = search.where("#{attr_prefixed} <= :query", query: query["to"]) if query["to"].present? end else if attr_type == "decimal" || attr_type == "number" query = query.to_f elsif attr_type == "integer" query = query.to_i end search = search.where("#{attr_prefixed} = :query", query: query) end when "boolean" search = search.where("#{attr_prefixed} = :query", query: to_boolean(query)) else raise Exception.new("Unable to query for attribute_type : #{attr_type}") end end end params[:filters].each do |attr, query| unless query.blank? if virtual_fields.present? && virtual_fields.include?(attr.to_s) search = search.select { |s| s.send(attr).present? ? s.send(attr).to_s.downcase.include?(query.downcase) : nil } end end end search end |
#handle_has_one ⇒ Object
164 165 166 167 168 169 170 171 172 |
# File 'app/controllers/slash_admin/models_controller.rb', line 164 def handle_has_one @has_one = {} Array.wrap(update_params + create_params).uniq.each do |p| if helpers.guess_field_type(@model_class.new, p) == "has_one" && !@model_class..key?(p.to_sym) @has_one[p] = permit_params[p] permit_params.delete(p) end end end |
#handle_specific_fields ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'app/controllers/slash_admin/models_controller.rb', line 174 def handle_specific_fields # has_one if @has_one.present? @has_one.each do |k, v| if v.present? @model.send("#{k}=", helpers.class_name_from_association(@model, k).constantize.find(v)) else @model.send("#{k}=", nil) end end end # JSON @model_class.columns_hash.each do |k, v| if permit_params[k].is_a? String if v.type == :json || v.type == :jsonb || helpers.serialized_json_field?(@model_class, k.to_s) begin @model.send("#{k}=", JSON.parse(permit_params[k])) rescue # Handle case when single string passed, we transform it into array to have a valid json json = permit_params[k].split(",").to_json @model.send("#{k}=", JSON.parse(json)) end end end end # Other end |
#index ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'app/controllers/slash_admin/models_controller.rb', line 16 def index :index, @model_class @models_export = if params[:filters].present? handle_filtered_search else @model_class.all end column = @model_class.arel_table[params[:order_field].to_sym] order = params[:order].downcase if %w[asc desc].include?(order) if @models_export.is_a?(Array) @models = if order == "asc" @models_export.sort { |m1, m2| m1.send(params[:order_field]) <=> m2.send(params[:order_field]) } else @models_export.sort { |m1, m2| m2.send(params[:order_field]) <=> m1.send(params[:order_field]) } end @pagy_models, @models = pagy_array(@models, items: params[:per]) else @pagy_models, @models = pagy(@models_export.order(column.send(params[:order].downcase)), items: params[:per]) end end @fields = if @use_export_params export_params else @model_class.column_names end respond_to do |format| format.html format.csv { stream_csv_report } format.xls { send_data render_to_string, filename: "#{@model_class.model_name.plural.upcase}_#{Date.today}.xls" } format.js { @models } end end |
#nestable ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'app/controllers/slash_admin/models_controller.rb', line 204 def nestable unless @is_nestable flash[:error] = t("slash_admin.controller.nestable.error", model_name: @model_name) redirect_to(main_app.polymorphic_url([:slash_admin, @model_class])) && return end if request.post? if params[:nestable][:data].present? JSON.parse(params[:nestable][:data]).each_with_index do |p, i| m = @model_class.find(p["id"]) m.position = i m.save! end end flash[:success] = t("slash_admin.controller.nestable.success") redirect_to(main_app.polymorphic_url(["slash_admin", @model_class])) && return if params.key?(:submit_redirect) redirect_to main_app.polymorphic_url([:nestable, :slash_admin, @model_class]) end end |
#new ⇒ Object
53 54 55 56 |
# File 'app/controllers/slash_admin/models_controller.rb', line 53 def new :new, @model_class @model = @model_class.new end |
#no_title ⇒ Object
160 161 162 |
# File 'app/controllers/slash_admin/models_controller.rb', line 160 def no_title [] end |
#show ⇒ Object
125 126 127 128 129 130 131 132 133 |
# File 'app/controllers/slash_admin/models_controller.rb', line 125 def show :show, @model_class @model = @model_class.find(params[:id]) respond_to do |format| format.html format.js { @model } end end |
#tooltips ⇒ Object
Add tooltip to th list view & edit/create view
attr: 'Value',
title: 'The title of my model',
156 157 158 |
# File 'app/controllers/slash_admin/models_controller.rb', line 156 def tooltips {} end |
#update ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/controllers/slash_admin/models_controller.rb', line 99 def update :edit, @model_class @model = @model_class.find(params[:id]) handle_has_one @model.assign_attributes(permit_params) before_validate_on_update handle_specific_fields if @model.valid? if @model.save! after_save_on_update flash[:success] = t("slash_admin.controller.update.success", model_name: @model_name) respond_to do |format| format.html { redirect_to(handle_redirect_after_submit) && return } format.js end end else flash[:error] = t("slash_admin.controller.update.error", model_name: @model_name) end render :edit end |
#update_params(options = {}) ⇒ Object
319 320 321 322 323 324 325 |
# File 'app/controllers/slash_admin/models_controller.rb', line 319 def update_params( = {}) if .present? create_params() else create_params end end |