templus_models 1.6.5 → 1.6.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: be235e07c0065d4c5a85c2f1aaec34e7e99c4474
4
- data.tar.gz: 28d9d080353e73cdc00c3f12750c6c64e4edc90f
3
+ metadata.gz: eae503daae66ce05e76fdfe3ecd11acaed24bb16
4
+ data.tar.gz: 2f4483b49c3e176d5625cbf356f16779f160b691
5
5
  SHA512:
6
- metadata.gz: b5f3146b2773659878fb772d6138791840f45114fba3b9fb3007ada4edb1e6d87ca766ec73ccec0f5f109b783b62ad3824179738e56ed0b055f1ce98752af871
7
- data.tar.gz: b46be38880c155a8853ff77e6d11d9f1dee39544bc4bddf394934987ffed35f0a8050c20fc91025698080870d87f02c821e7677b040fe245e6d937048bc45199
6
+ metadata.gz: 6ef51d8bb46956f4bebb9e6c3dc40ea9d700c4727f09afe01e7da7608a77daa9c760c61458a1e10e036790ae6b3b5321bc9a7fdd40972706403d98f5bfd271af
7
+ data.tar.gz: d84354d547bc57ab597b573257226ac639883ebe68ebea13f04374889fdc8d7d96e8ad353e57a92b7b86c1bccdba61b4219313c289fd68df3366944fa3f598a3
@@ -1,31 +1,9 @@
1
1
  class CrudController < ApplicationController
2
2
  before_filter :setup, except: :autocomplete
3
3
 
4
- private
5
- def setup
6
- if params[:associacao]
7
- @crud_associacao = Module.const_get("#{params[:model].to_s.singularize}_crud".camelize)
8
- @model = Module.const_get(params[:model].camelize).find(params[:id]).send(params[:associacao])
9
- c_helper = Module.const_get(params[:model].camelize).reflect_on_association(params[:associacao]).class_name
10
- @crud_helper = Module.const_get("#{c_helper}Crud") unless params[:render] == "modal" and params[:action] == "new"
11
- @url = crud_associacao_models_path(model: params[:model], id: params[:id], associacao: params[:associacao], page: params[:page], q: params[:q])
12
- @clean_url = crud_associacao_models_path(model: params[:model], id: params[:id], associacao: params[:associacao])
13
- @model_permission = c_helper.constantize
14
- @id = params[:associacao_id] if params[:associacao_id]
15
- else
16
- @model = Module.const_get(params[:model].camelize)
17
- @model_permission = @model
18
- @crud_helper = Module.const_get("#{params[:model]}_crud".camelize) unless params[:render] == "modal" and params[:action] == "new"
19
- @url = crud_models_path(model: params[:model], page: params[:page], q: params[:q])
20
- @clean_url = crud_models_path(model: params[:model])
21
- @id = params[:id] if params[:id]
22
- end
23
- end
24
-
25
- public
26
4
  def index
27
5
  authorize! :read, @model_permission if respond_to?(:current_usuario)
28
- if params[:scope].present?
6
+ if params[:scope].present? && valid_method?(params[:scope])
29
7
  @q = @model.send(params[:scope]).search(params[:q])
30
8
  else
31
9
  @q = @model.search(params[:q])
@@ -75,7 +53,7 @@ class CrudController < ApplicationController
75
53
  def action
76
54
  @record = @model.find(@id)
77
55
  authorize! :create_or_update, @record if respond_to?(:current_usuario)
78
- if @model.method_defined?(params[:acao])
56
+ if valid_instance_method?(params[:acao])
79
57
  if @record.send(params[:acao])
80
58
  flash.now[:success] = "Ação #{params[:acao]} efetuada com sucesso."
81
59
  else
@@ -166,7 +144,11 @@ class CrudController < ApplicationController
166
144
  else
167
145
  results = @q.result.page(params[:page])
168
146
  end
169
- method_label = params[:label]
147
+ if valid_instance_method?(params[:label])
148
+ method_label = params[:label]
149
+ else
150
+ raise "Ação inválida"
151
+ end
170
152
  render json: results.map {|result| {id: result.id, label: result.send(method_label), value: result.send(method_label)} }
171
153
  end
172
154
 
@@ -196,6 +178,30 @@ class CrudController < ApplicationController
196
178
  end
197
179
 
198
180
  private
181
+ def setup
182
+ if params[:associacao]
183
+ @crud_associacao = Module.const_get("#{params[:model].to_s.singularize}_crud".camelize)
184
+ if Module.const_get(params[:model].camelize).reflect_on_association(params[:associacao])
185
+ @model = Module.const_get(params[:model].camelize).find(params[:id]).send(params[:associacao])
186
+ else
187
+ raise "Ação inválida"
188
+ end
189
+ c_helper = Module.const_get(params[:model].camelize).reflect_on_association(params[:associacao]).class_name
190
+ @crud_helper = Module.const_get("#{c_helper}Crud") unless params[:render] == "modal" and params[:action] == "new"
191
+ @url = crud_associacao_models_path(model: params[:model], id: params[:id], associacao: params[:associacao], page: params[:page], q: params[:q])
192
+ @clean_url = crud_associacao_models_path(model: params[:model], id: params[:id], associacao: params[:associacao])
193
+ @model_permission = c_helper.constantize
194
+ @id = params[:associacao_id] if params[:associacao_id]
195
+ else
196
+ @model = Module.const_get(params[:model].camelize)
197
+ @model_permission = @model
198
+ @crud_helper = Module.const_get("#{params[:model]}_crud".camelize) unless params[:render] == "modal" and params[:action] == "new"
199
+ @url = crud_models_path(model: params[:model], page: params[:page], q: params[:q])
200
+ @clean_url = crud_models_path(model: params[:model])
201
+ @id = params[:id] if params[:id]
202
+ end
203
+ end
204
+
199
205
  def params_permitt
200
206
  params.require(@model.name.underscore.to_sym).permit(fields_model)
201
207
  end
@@ -251,4 +257,22 @@ class CrudController < ApplicationController
251
257
  end
252
258
  group
253
259
  end
260
+
261
+ def valid_method?(method)
262
+ list_methods = []
263
+ @model.ancestors.each do |m|
264
+ list_methods << m.methods(false).reject{ |m| /^_/ =~ m.to_s }
265
+ break if m.superclass.to_s == "ActiveRecord::Base"
266
+ end
267
+ list_methods.flatten.include? method.to_sym
268
+ end
269
+
270
+ def valid_instance_method?(method)
271
+ list_methods = []
272
+ @model.ancestors.each do |m|
273
+ list_methods << m.instance_methods(false).reject{ |m| /^_/ =~ m.to_s }
274
+ break if m.superclass.to_s == "ActiveRecord::Base"
275
+ end
276
+ list_methods.flatten.include? method.to_sym
277
+ end
254
278
  end
@@ -1,3 +1,3 @@
1
1
  module TemplusModels
2
- VERSION = "1.6.5"
2
+ VERSION = "1.6.6"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: templus_models
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.5
4
+ version: 1.6.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Sol
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2017-02-13 00:00:00.000000000 Z
13
+ date: 2017-04-12 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -977,7 +977,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
977
977
  version: '0'
978
978
  requirements: []
979
979
  rubyforge_project:
980
- rubygems_version: 2.4.6
980
+ rubygems_version: 2.5.1
981
981
  signing_key:
982
982
  specification_version: 4
983
983
  summary: Easy CRUD generator for Rails Projects