Class: CrudController

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

Instance Method Summary collapse

Instance Method Details

#actionObject



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

def action
  authorize! :create_or_update, @model if respond_to?(:current_usuario)
  @record = @model.find(params[:id])
  if @model.method_defined?(params[:acao])
    if @record.send(params[:acao])
      flash.now[:success] = "Ação #{params[:acao]} efetuada com sucesso."
    else
      flash.now[:error] = "Erro ao tentar executar a ação #{params[:acao]}."
    end
    index
  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



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/controllers/crud_controller.rb', line 136

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.search(parametros)
  @q.sorts = 'updated_at desc' if @q.sorts.empty?
  if respond_to?(:current_usuario)
    results = @q.result(distinct: true).accessible_by(current_ability).page(params[:page])
  else
    results = @q.result(distinct: true).page(params[:page])
  end
  method_label = params[:label]
  render json: results.map {|result| {id: result.id, label: result.send(method_label), value: result.send(method_label)} }
end

#createObject



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 72

def create
  authorize! :create, @model if respond_to?(:current_usuario)
  @saved = false
  if params[:id]
    @record = @model.find(params[:id])
    @saved = @record.update(params_permitt)
  else
    @record  =  @model.new(params_permitt)
    @saved = @record.save
  end
  
  respond_to do |format|
    if @saved
      flash[:success] = params[:id].present? ? "Cadastro alterado com sucesso." : "Cadastro efetuado com sucesso."
      format.html { redirect_to "/crud/#{@model.name.underscore}?page=#{params[:page]}" }
      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
  authorize! :destroy, @model if respond_to?(:current_usuario)
  @record = @model.find(params[:id])
  if @record.destroy
    respond_to do |format|
      flash[:success] = "Cadastro removido com sucesso."
      format.html { redirect_to "/crud/#{@model.name.underscore}" }
      format.js { render action: :index }
    end
  else
    respond_to do |format|
      flash[:error] = @record.errors.full_messages.join(", ")
      format.html { redirect_to "/crud/#{@model.name.underscore}" }
      format.js { render action: :index }
    end
  end
end

#editObject



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

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

#indexObject



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

def index
  authorize! :read, @model if respond_to?(:current_usuario)
  if params[:scope].present?
    @q = @model.send(params[:scope]).search(params[:q])
  else
    @q = @model.search(params[:q])
  end
  if @q.sorts.empty?
    if "#{@crud_helper.order_field}".include?("desc") or "#{@crud_helper.order_field}".include?("asc")
      @q.sorts = "#{@crud_helper.order_field}"
    else
      @q.sorts = "#{@crud_helper.order_field} asc"
    end
  end
  if respond_to?(:current_usuario)
    @records = @q.result(distinct: true).accessible_by(current_ability, :read).page(params[:page]).per(@crud_helper.per_page)
  else
    @records = @q.result(distinct: true).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

#newObject



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

def new
  authorize! :create, @model if respond_to?(:current_usuario)
  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
  end
  @crud_helper = Module.const_get("#{@model}Crud".camelize)
  @record = @model.new
end

#queryObject



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

def query
  authorize! :read, @model if respond_to?(:current_usuario)
  @resource = Module.const_get(params[:model].classify)
  @q = @resource.search(params[:q])
  @q.sorts = 'updated_at desc' if @q.sorts.empty?
  if respond_to?(:current_usuario)
    results = @q.result(distinct: true).accessible_by(current_ability).page(params[:page])
  else
    results = @q.result(distinct: true).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

#setupObject



27
28
29
30
# File 'app/controllers/crud_controller.rb', line 27

def setup
  @model = Module.const_get(params[:model].camelize)
  @crud_helper = Module.const_get("#{params[:model]}_crud".camelize)
end

#showObject



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

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