Class: SlashAdmin::ModelsController
- Inherits:
-
BaseController
- Object
- ActionController::Base
- ApplicationController
- BaseController
- SlashAdmin::ModelsController
- 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
- #show ⇒ Object
-
#tooltips ⇒ Object
Add tooltip to th list 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
60 61 |
# File 'app/controllers/slash_admin/models_controller.rb', line 60 def after_save_on_create end |
#after_save_on_update ⇒ Object
146 147 |
# File 'app/controllers/slash_admin/models_controller.rb', line 146 def after_save_on_update end |
#autocomplete_params ⇒ Object
321 322 323 324 325 326 327 328 329 |
# File 'app/controllers/slash_admin/models_controller.rb', line 321 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
57 58 |
# File 'app/controllers/slash_admin/models_controller.rb', line 57 def before_validate_on_create end |
#before_validate_on_update ⇒ Object
143 144 |
# File 'app/controllers/slash_admin/models_controller.rb', line 143 def before_validate_on_update end |
#create ⇒ Object
63 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 |
# File 'app/controllers/slash_admin/models_controller.rb', line 63 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
134 135 136 137 138 139 140 141 |
# File 'app/controllers/slash_admin/models_controller.rb', line 134 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
93 94 95 96 |
# File 'app/controllers/slash_admin/models_controller.rb', line 93 def edit :edit, @model_class @model = @model_class.find(params[:id]) end |
#export_csv(options = {}) ⇒ Object
Export CSV
309 310 311 |
# File 'app/controllers/slash_admin/models_controller.rb', line 309 def export_csv( = {}) @models_export.to_sql end |
#handle_filtered_search ⇒ Object
221 222 223 224 225 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 |
# File 'app/controllers/slash_admin/models_controller.rb', line 221 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 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
159 160 161 162 163 164 165 166 167 |
# File 'app/controllers/slash_admin/models_controller.rb', line 159 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
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'app/controllers/slash_admin/models_controller.rb', line 169 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
14 15 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 |
# File 'app/controllers/slash_admin/models_controller.rb', line 14 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) attributes = @model_class.new.attributes.keys if (!attributes.include?(params[:order_field]) && @model_class.method_defined?(params[:order_field])) || @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 @models = Kaminari.paginate_array(@models).page(params[:page]).per(params[:per]) else @models = @models_export.order(column.send(params[:order].downcase)).page(params[:page]).per(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
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'app/controllers/slash_admin/models_controller.rb', line 199 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
52 53 54 55 |
# File 'app/controllers/slash_admin/models_controller.rb', line 52 def new :new, @model_class @model = @model_class.new end |
#show ⇒ Object
124 125 126 127 128 129 130 131 132 |
# File 'app/controllers/slash_admin/models_controller.rb', line 124 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
attr: 'Value',
title: 'The title of my model',
155 156 157 |
# File 'app/controllers/slash_admin/models_controller.rb', line 155 def tooltips {} end |
#update ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'app/controllers/slash_admin/models_controller.rb', line 98 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
313 314 315 316 317 318 319 |
# File 'app/controllers/slash_admin/models_controller.rb', line 313 def update_params( = {}) if .present? create_params() else create_params end end |