dynamic_controller 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/dynamic_controller.gemspec +1 -1
- data/lib/dynamic_controller/helper_methods.rb +33 -0
- data/lib/dynamic_controller/instance_methods.rb +15 -12
- data/lib/dynamic_controller/version.rb +1 -1
- data/spec/controllers/crud_actions_html_spec.rb +0 -1
- data/spec/controllers/nested_crud_actions_html_spec.rb +0 -1
- data/spec/controllers/ransack_spec.rb +58 -0
- data/spec/controllers/two_level_nested_crud_actions_html_spec.rb +0 -1
- data/spec/dummy/Gemfile +13 -12
- metadata +18 -17
data/dynamic_controller.gemspec
CHANGED
@@ -55,5 +55,38 @@ module DynamicController
|
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
|
+
def model_extended_attributes
|
59
|
+
resource_class.column_names | resource_class.reflections.map { |k, v| v.klass.column_names.map { |c| "#{k}_#{c}" } }.flatten
|
60
|
+
end
|
61
|
+
|
62
|
+
def search_query
|
63
|
+
params[:q].is_a?(String) ? NQL.to_ransack(params[:q]) : params[:q]
|
64
|
+
end
|
65
|
+
|
66
|
+
def search_query_valid?
|
67
|
+
begin
|
68
|
+
search_node_valid? search_query, model_extended_attributes
|
69
|
+
rescue NQL::InvalidExpressionError => ex
|
70
|
+
Rails.logger.debug "Invalid search query: #{params[:q]} | Error: #{ex.message}"
|
71
|
+
false
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def search_node_valid?(node, valid_attributes)
|
76
|
+
return true unless node
|
77
|
+
node.deep_symbolize_keys.each do |k, v|
|
78
|
+
if k == :a
|
79
|
+
return false unless valid_attributes.include?(v['0'.to_sym][:name])
|
80
|
+
else
|
81
|
+
if v.is_a?(Hash)
|
82
|
+
return false unless search_node_valid?(v, valid_attributes)
|
83
|
+
elsif v.is_a?(Array)
|
84
|
+
v.select { |e| e.is_a?(Hash) }.each { |e| return false unless search_node_valid?(e, valid_attributes) }
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
true
|
89
|
+
end
|
90
|
+
|
58
91
|
end
|
59
92
|
end
|
@@ -6,21 +6,24 @@ module DynamicController
|
|
6
6
|
|
7
7
|
if base.include_action?(:index)
|
8
8
|
base.send :define_method, :index do
|
9
|
-
begin
|
10
|
-
query = params[:q].is_a?(String) ? NQL.to_ransack(params[:q]) : params[:q]
|
11
|
-
valid_query = true
|
12
|
-
rescue NQL::InvalidExpressionError => ex
|
13
|
-
Rails.logger.debug "Invalid search expression: #{params[:q]} | Error: #{ex.message}"
|
14
|
-
query = nil
|
15
|
-
valid_query = false
|
16
|
-
end
|
17
9
|
if parent_model
|
18
|
-
|
10
|
+
model = parent_model.send(controller_name)
|
11
|
+
else
|
12
|
+
model = resource_class
|
13
|
+
end
|
14
|
+
|
15
|
+
empty_collection = model.where('1=2').page(params[:page])
|
16
|
+
|
17
|
+
if search_query_valid?
|
18
|
+
begin
|
19
|
+
self.collection = model.search(search_query).result(distinct: true).page(params[:page])
|
20
|
+
rescue ActiveRecord::StatementInvalid => ex
|
21
|
+
Rails.logger.error "Invalid search query: #{params[:q]} | Error: #{ex.message}"
|
22
|
+
self.collection = empty_collection
|
23
|
+
end
|
19
24
|
else
|
20
|
-
|
25
|
+
self.collection = empty_collection
|
21
26
|
end
|
22
|
-
self.collection = @search.result(distinct: true).page(params[:page])
|
23
|
-
self.collection = self.collection.where('1=2') unless valid_query
|
24
27
|
|
25
28
|
Responder.new(self).index
|
26
29
|
end
|
@@ -11,7 +11,6 @@ describe CountriesController, '-> HTML', type: :controller do
|
|
11
11
|
response.content_type.should eq 'text/html'
|
12
12
|
response.should render_template :index
|
13
13
|
assigns(:countries).should eq Country.all
|
14
|
-
assigns(:search).should be_a Ransack::Search
|
15
14
|
end
|
16
15
|
|
17
16
|
it 'Show -> GET /resources/:id' do
|
@@ -13,7 +13,6 @@ describe CitiesController, '-> HTML', type: :controller do
|
|
13
13
|
response.should render_template :index
|
14
14
|
assigns(:cities).should eq City.all
|
15
15
|
assigns(:country).should eq country
|
16
|
-
assigns(:search).should be_a Ransack::Search
|
17
16
|
end
|
18
17
|
|
19
18
|
it 'Show -> GET /parent_resource/:parent_id/resources/:id' do
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CountriesController, '-> Ransack query search', type: :controller do
|
4
|
+
|
5
|
+
it 'Filter by q[name_eq]' do
|
6
|
+
3.times { create :country }
|
7
|
+
|
8
|
+
country = Country.first
|
9
|
+
|
10
|
+
get :index, q: {name_eq: country.name}
|
11
|
+
|
12
|
+
response.should be_success
|
13
|
+
assigns(:countries).should eq [country]
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'Filter by attribute, predicate and value' do
|
17
|
+
3.times { create :country }
|
18
|
+
|
19
|
+
country = Country.first
|
20
|
+
|
21
|
+
get :index, q: {c: [{a: {'0' => {name: 'name'}}, p: 'eq', v: {'0' => {value: country.name}}}]}
|
22
|
+
|
23
|
+
response.should be_success
|
24
|
+
assigns(:countries).should eq [country]
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'Filter by NQL' do
|
28
|
+
3.times { create :country }
|
29
|
+
|
30
|
+
country = Country.first
|
31
|
+
|
32
|
+
get :index, q: "name = #{country.name}"
|
33
|
+
|
34
|
+
response.should be_success
|
35
|
+
assigns(:countries).should eq [country]
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'Filter by NQL with invalid field' do
|
39
|
+
3.times { create :country }
|
40
|
+
|
41
|
+
country = Country.first
|
42
|
+
|
43
|
+
get :index, q: "abcd = #{country.name}"
|
44
|
+
|
45
|
+
response.should be_success
|
46
|
+
assigns(:countries).should eq []
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'Filter by NQL with invalid expression' do
|
50
|
+
3.times { create :country }
|
51
|
+
|
52
|
+
get :index, q: 'abcd = '
|
53
|
+
|
54
|
+
response.should be_success
|
55
|
+
assigns(:countries).should eq []
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
@@ -14,7 +14,6 @@ describe StreetsController, '-> HTML', type: :controller do
|
|
14
14
|
assigns(:streets).should eq Street.all
|
15
15
|
assigns(:city).should eq city
|
16
16
|
assigns(:country).should eq city.country
|
17
|
-
assigns(:search).should be_a Ransack::Search
|
18
17
|
end
|
19
18
|
|
20
19
|
it 'Show -> GET /first_resource/:first_id/second_resource/:second_id/resources/:id' do
|
data/spec/dummy/Gemfile
CHANGED
@@ -1,13 +1,14 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
gem 'rails', '3.2.8'
|
4
|
-
gem 'sqlite3'
|
5
|
-
gem 'dynamic_controller', path: "#{File.dirname(__FILE__)}../../../"
|
6
|
-
|
7
|
-
group :assets do
|
8
|
-
gem 'sass-rails', '~> 3.2.3'
|
9
|
-
gem 'coffee-rails', '~> 3.2.1'
|
10
|
-
gem 'uglifier', '>= 1.0.3'
|
11
|
-
|
12
|
-
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gem 'rails', '3.2.8'
|
4
|
+
gem 'sqlite3'
|
5
|
+
gem 'dynamic_controller', path: "#{File.dirname(__FILE__)}../../../"
|
6
|
+
|
7
|
+
group :assets do
|
8
|
+
gem 'sass-rails', '~> 3.2.3'
|
9
|
+
gem 'coffee-rails', '~> 3.2.1'
|
10
|
+
gem 'uglifier', '>= 1.0.3'
|
11
|
+
gem 'therubyracer' if RUBY_PLATFORM.include? 'linux'
|
12
|
+
end
|
13
|
+
|
13
14
|
gem 'jquery-rails'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dynamic_controller
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11-
|
12
|
+
date: 2012-11-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: ransack
|
16
|
-
requirement: &
|
16
|
+
requirement: &28077168 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *28077168
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: kaminari
|
27
|
-
requirement: &
|
27
|
+
requirement: &28076916 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,21 +32,21 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *28076916
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: nql
|
38
|
-
requirement: &
|
38
|
+
requirement: &28076616 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - =
|
42
42
|
- !ruby/object:Gem::Version
|
43
|
-
version: 0.0.
|
43
|
+
version: 0.0.4
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *28076616
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rails
|
49
|
-
requirement: &
|
49
|
+
requirement: &28076364 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *28076364
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: sqlite3
|
60
|
-
requirement: &
|
60
|
+
requirement: &28076088 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *28076088
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rspec-rails
|
71
|
-
requirement: &
|
71
|
+
requirement: &28075836 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ! '>='
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: '0'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *28075836
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: factory_girl_rails
|
82
|
-
requirement: &
|
82
|
+
requirement: &28075584 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ! '>='
|
@@ -87,7 +87,7 @@ dependencies:
|
|
87
87
|
version: '0'
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *28075584
|
91
91
|
description: Simple way to add CRUD actions into Rails controllers
|
92
92
|
email:
|
93
93
|
- gabynaiman@gmail.com
|
@@ -113,6 +113,7 @@ files:
|
|
113
113
|
- spec/controllers/crud_actions_json_spec.rb
|
114
114
|
- spec/controllers/nested_crud_actions_html_spec.rb
|
115
115
|
- spec/controllers/nested_crud_actions_json_spec.rb
|
116
|
+
- spec/controllers/ransack_spec.rb
|
116
117
|
- spec/controllers/redefined_responders_html_spec.rb
|
117
118
|
- spec/controllers/redefined_responders_json_spec.rb
|
118
119
|
- spec/controllers/two_level_nested_crud_actions_html_spec.rb
|