Class: CrudController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/crud_controller.rb

Instance Method Summary collapse

Instance Method Details

#actionObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/crud_controller.rb', line 54

def action
  @record = @model.find(@id)
  authorize! :create_or_update, @record if respond_to?(:current_usuario)
  if valid_instance_method?(params[:acao])
    if @record.send(params[:acao])
      flash.now[:success] = I18n.t("mensagem_action", acao: params[:acao])
    else
      flash.now[:error] = I18n.t("mensagem_erro_action", acao: params[:acao])
    end
    redirect_to @url
  else
    @titulo = @record.to_s
    @texto = params[:acao]
    render partial: "/#{@model.name.underscore.pluralize}/#{params[:acao]}" if request.respond_to?(:wiselinks_partial?) && request.wiselinks_partial?
  end
end

#autocompleteObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'app/controllers/crud_controller.rb', line 140

def autocomplete
  @model = Module.const_get(params[:model].camelize)
  authorize! :read, @model if respond_to?(:current_usuario)
  parametros = {}
  parametros["#{params[:campo]}_#{params[:tipo]}"] = params[:term]
  @q = @model.ransack(parametros)
  @q.sorts = 'updated_at desc' if @q.sorts.empty?
  if respond_to?(:current_usuario)
    results = @q.result.accessible_by(current_ability).page(params[:page])
  else
    results = @q.result.page(params[:page])
  end
  if valid_instance_method?(params[:label])
    method_label = params[:label]
  else
    raise "Ação inválida"
  end
  render json: results.map {|result| {id: result.id, label: result.send(method_label), value: result.send(method_label)} }
end

#createObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/crud_controller.rb', line 71

def create
  @saved = false
  if @id
    @record = @model.find(@id)
    authorize! :update, @record if respond_to?(:current_usuario)
    @saved = @record.update(params_permitt)
  else
    @record  =  @model.new(params_permitt)
    authorize! :create, @model_permission if respond_to?(:current_usuario)
    @saved = @record.save
  end

  respond_to do |format|
    if @saved
      flash[:success] = params[:id].present? ? I18n.t("updated", model: I18n.t("model.#{@model.name.underscore}")) : I18n.t("created", model: I18n.t("model.#{@model.name.underscore}"))
      format.html { redirect_to @url }
      unless params[:render] == 'modal'
        format.js { render action: :index}
      else
        format.js
      end
    else
      action = (params[:id]) ? :edit : :new
      format.html { render action: action }
      format.js
    end
  end
end

#destroyObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/controllers/crud_controller.rb', line 100

def destroy
  @record = @model.find(@id)
  authorize! :destroy, @record if respond_to?(:current_usuario)
  if @record.destroy
    respond_to do |format|
      flash[:success] = I18n.t("destroyed", model: I18n.t("model.#{@model.name.underscore}"))
      format.html { redirect_to @url }
      format.js { render action: :index }
    end
  else
    respond_to do |format|
      flash[:error] = @record.errors.full_messages.join(", ")
      format.html { redirect_to @url }
      format.js { render action: :index }
    end
  end
end

#editObject



44
45
46
47
# File 'app/controllers/crud_controller.rb', line 44

def edit
  @record = @model.find(@id)
  authorize! :edit, @record if respond_to?(:current_usuario)
end

#indexObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/crud_controller.rb', line 5

def index
  authorize! :read, @model_permission if respond_to?(:current_usuario)
  if params[:scope].present? && valid_method?(params[:scope])
    @q = @model.send(params[:scope]).ransack(params[:q])
  else
    @q = @model.ransack(params[:q])
  end
  if @q.sorts.empty?
    if @crud_helper.order_field.to_s.include?("desc") || @crud_helper.order_field.to_s.include?("asc")
      @q.sorts = @crud_helper.order_field.to_s
    else
      @q.sorts = "#{@crud_helper.order_field} asc"
    end
  end
  if respond_to?(:current_usuario)
    @records = @q.result(distinct: true).includes(@crud_helper.includes).accessible_by(current_ability, :read).page(params[:page]).per(@crud_helper.per_page)
  else
    @records = @q.result(distinct: true).includes(@crud_helper.includes).page(params[:page]).per(@crud_helper.per_page)
  end
  @titulo = @model.name.pluralize
  render partial: 'records' if request.respond_to?(:wiselinks_partial?) && request.wiselinks_partial?
end

#listingObject



160
161
162
163
164
165
166
167
168
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
# File 'app/controllers/crud_controller.rb', line 160

def listing
  authorize! :read, @model_permission if respond_to?(:current_usuario)
  if params[:scope].present? && valid_method?(params[:scope])
    @q = @model.send(params[:scope]).ransack(params[:q])
  else
    @q = @model.ransack(params[:q])
  end
  if @q.sorts.empty?
    if @crud_helper.order_field.to_s.include?("desc") || @crud_helper.order_field.to_s.include?("asc")
      @q.sorts = @crud_helper.order_field.to_s
    else
      @q.sorts = "#{@crud_helper.order_field} asc"
    end
  end
  if respond_to?(:current_usuario)
    @records = @q.result.accessible_by(current_ability)
  else
    @records = @q.result
  end
  report_name = "#{@crud_helper.title}_#{DateTime.now.strftime('%Y%m%d')}"
  respond_to do |format|
    format.xls { headers["Content-Disposition"] = "attachment; filename=#{report_name}.xls" }
    format.pdf do
      pdf = WickedPdf.new.pdf_from_string(
        render_to_string('crud/listing.pdf.erb'),
        encoding: 'UTF-8',
        page_size: 'A4',
        show_as_html: params[:debug],
        margin: { top: 20, bottom: 20 }
      )
      send_data(pdf, filename: "#{report_name}.pdf", type: "application/pdf", disposition: "inline")
    end
  end
end

#newObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/crud_controller.rb', line 28

def new
  if params[:render] == "modal"
    if @model.reflect_on_association(params[:attribute].to_s).present?
      @model = @model.reflect_on_association(params[:attribute].to_s).class_name.constantize
    else
      @model = params[:attribute].to_s.camelcase.constantize
    end
    @url = crud_models_path(model: @model.name.underscore)
    @clean_url = @url
    @model_permission = @model
    @crud_helper = Module.const_get("#{@model}Crud".camelize)
  end
  authorize! :new, @model_permission if respond_to?(:current_usuario)
  @record = @model.new
end

#printingObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'app/controllers/crud_controller.rb', line 195

def printing
  @record = @model.find(@id)
  authorize! :read, @record if respond_to?(:current_usuario)
  report_name = "#{@record}_#{DateTime.now.strftime('%Y%m%d')}"
  respond_to do |format|
    format.pdf do
      pdf = WickedPdf.new.pdf_from_string(
        render_to_string('crud/printing.pdf.erb'),
        encoding: 'UTF-8',
        page_size: 'A4',
        show_as_html: params[:debug],
        margin: { top: 20, bottom: 20 }
      )
      send_data(pdf, filename: "#{report_name}.pdf", type: "application/pdf", disposition: "inline")
    end
    format.html
  end
end

#queryObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/controllers/crud_controller.rb', line 118

def query
  authorize! :read, @model_permission if respond_to?(:current_usuario)
  @resource = @model
  if params[:scope].present? && valid_method?(params[:scope])
    @q = @model.send(params[:scope]).ransack(params[:q])
  else
    @q = @model.ransack(params[:q])
  end
  @q.sorts = 'updated_at desc' if @q.sorts.empty?
  if respond_to?(:current_usuario)
    results = @q.result.accessible_by(current_ability).page(params[:page])
  else
    results = @q.result.page(params[:page])
  end
  instance_variable_set("@#{params[:var]}", results)
  if request.respond_to?(:wiselinks_partial?) && request.wiselinks_partial?
    render :partial => params[:partial]
  else
    render :index, controller: request[:controller]
  end
end

#showObject



49
50
51
52
# File 'app/controllers/crud_controller.rb', line 49

def show
  @record = @model.find(@id)
  authorize! :read, @record if respond_to?(:current_usuario)
end