dynamic_controller 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/README.md +80 -80
  2. data/dynamic_controller.gemspec +27 -27
  3. data/lib/dynamic_controller/action_controller_extension.rb +18 -16
  4. data/lib/dynamic_controller/class_methods.rb +41 -41
  5. data/lib/dynamic_controller/helper_methods.rb +103 -91
  6. data/lib/dynamic_controller/instance_methods.rb +139 -139
  7. data/lib/dynamic_controller/resource.rb +21 -21
  8. data/lib/dynamic_controller/responder.rb +70 -70
  9. data/lib/dynamic_controller/version.rb +3 -3
  10. data/lib/dynamic_controller.rb +18 -18
  11. data/spec/controller_factory.rb +22 -22
  12. data/spec/controllers/crud_actions_html_spec.rb +111 -111
  13. data/spec/controllers/crud_actions_json_spec.rb +91 -91
  14. data/spec/controllers/nested_crud_actions_html_spec.rb +125 -125
  15. data/spec/controllers/nested_crud_actions_json_spec.rb +97 -97
  16. data/spec/controllers/ransack_spec.rb +57 -57
  17. data/spec/controllers/redefined_responders_html_spec.rb +111 -111
  18. data/spec/controllers/redefined_responders_json_spec.rb +94 -94
  19. data/spec/controllers/two_level_nested_crud_actions_html_spec.rb +133 -133
  20. data/spec/controllers/two_level_nested_crud_actions_json_spec.rb +97 -97
  21. data/spec/custom_responder_format_spec.rb +12 -12
  22. data/spec/dummy/Gemfile +13 -13
  23. data/spec/dummy/app/controllers/cities_controller.rb +95 -95
  24. data/spec/dummy/app/controllers/languages_controller.rb +95 -95
  25. data/spec/dummy/app/controllers/streets_controller.rb +97 -97
  26. data/spec/dummy/app/models/city.rb +6 -6
  27. data/spec/dummy/app/models/language.rb +4 -4
  28. data/spec/dummy/app/models/street.rb +5 -5
  29. data/spec/dummy/app/views/cities/_form.html.erb +25 -25
  30. data/spec/dummy/app/views/cities/index.html.erb +29 -29
  31. data/spec/dummy/app/views/countries/index.html.erb +25 -25
  32. data/spec/dummy/app/views/languages/_form.html.erb +21 -21
  33. data/spec/dummy/app/views/languages/edit.html.erb +6 -6
  34. data/spec/dummy/app/views/languages/index.html.erb +23 -23
  35. data/spec/dummy/app/views/languages/new.html.erb +5 -5
  36. data/spec/dummy/app/views/languages/show.html.erb +10 -10
  37. data/spec/dummy/app/views/streets/_form.html.erb +25 -25
  38. data/spec/dummy/app/views/streets/edit.html.erb +6 -6
  39. data/spec/dummy/app/views/streets/index.html.erb +27 -27
  40. data/spec/dummy/app/views/streets/new.html.erb +5 -5
  41. data/spec/dummy/app/views/streets/show.html.erb +15 -15
  42. data/spec/dummy/config/routes.rb +8 -8
  43. data/spec/dummy/db/migrate/20120922010743_create_languages.rb +9 -9
  44. data/spec/dummy/db/migrate/20120929185302_create_streets.rb +11 -11
  45. data/spec/dummy/db/schema.rb +46 -46
  46. data/spec/factories.rb +21 -21
  47. data/spec/has_crud_actions_options_spec.rb +48 -48
  48. data/spec/spec_helper.rb +35 -35
  49. metadata +58 -17
@@ -1,140 +1,140 @@
1
- module DynamicController
2
- module InstanceMethods
3
-
4
- def self.included(base)
5
- base.rescue_from StandardError, with: :handle_error
6
-
7
- if base.include_action?(:index)
8
- base.send :define_method, :index do
9
- if parent_model
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
- self.collection.all #Force load to handle exception here
21
- rescue ActiveRecord::StatementInvalid => ex
22
- Rails.logger.debug "Invalid search query: #{params[:q]} | Error: #{ex.message}"
23
- self.collection = empty_collection
24
- end
25
- else
26
- self.collection = empty_collection
27
- end
28
-
29
- Responder.new(self).index
30
- end
31
- end
32
-
33
- if base.include_action?(:show)
34
- base.send :define_method, :show do
35
- if parent_model
36
- self.model = parent_model.send(controller_name).find(params[:id])
37
- else
38
- self.model = resource_class.find(params[:id])
39
- end
40
-
41
- Responder.new(self).show
42
- end
43
- end
44
-
45
- if base.include_action?(:new)
46
- base.send :define_method, :new do
47
- if parent_model
48
- self.model = parent_model.send(controller_name).build
49
- else
50
- self.model = resource_class.new
51
- end
52
-
53
- Responder.new(self).new
54
- end
55
- end
56
-
57
- if base.include_action?(:edit)
58
- base.send :define_method, :edit do
59
- if parent_model
60
- self.model = parent_model.send(controller_name).find(params[:id])
61
- else
62
- self.model = resource_class.find(params[:id])
63
- end
64
-
65
- Responder.new(self).edit
66
- end
67
- end
68
-
69
- if base.include_action?(:create)
70
- base.send :define_method, :create do
71
- if parent_model
72
- self.model = parent_model.send(controller_name).build(params[controller_name.singularize])
73
- else
74
- self.model = resource_class.new(params[controller_name.singularize])
75
- end
76
-
77
- if model.save
78
- flash_message = "#{resource_class.model_name.human} successfully created"
79
- if params[:save_and_new]
80
- flash[:success] = flash_message
81
- redirect_to action: :new
82
- else
83
- flash.now[:success] = flash_message
84
- Responder.new(self).create
85
- end
86
- else
87
- respond_to do |format|
88
- format.html { render :new }
89
- format.json { render json: model.errors, status: :unprocessable_entity }
90
- end
91
- end
92
- end
93
- end
94
-
95
- if base.include_action?(:update)
96
- base.send :define_method, :update do
97
- if parent_model
98
- self.model = parent_model.send(controller_name).find(params[:id])
99
- else
100
- self.model = resource_class.find(params[:id])
101
- end
102
-
103
- if model.update_attributes(params[controller_name.singularize])
104
- flash.now[:success] = "#{resource_class.model_name.human} successfully updated"
105
- Responder.new(self).update
106
- else
107
- respond_to do |format|
108
- format.html { render :edit }
109
- format.json { render json: model.errors, status: :unprocessable_entity }
110
- end
111
- end
112
- end
113
-
114
- end
115
-
116
- if base.include_action?(:destroy)
117
- base.send :define_method, :destroy do
118
- if parent_model
119
- self.model = parent_model.send(controller_name).find(params[:id])
120
- else
121
- self.model = resource_class.find(params[:id])
122
- end
123
-
124
- if model.destroy
125
- flash[:warning] = "#{resource_class.model_name.human} successfully removed"
126
- Responder.new(self).destroy
127
- else
128
- flash[:danger] = "#{resource_class.model_name.human} could not be deleted"
129
- respond_to do |format|
130
- format.html { redirect_to action: :index }
131
- format.json { render json: model.errors, status: :unprocessable_entity }
132
- end
133
- end
134
- end
135
- end
136
-
137
- end
138
-
139
- end
1
+ module DynamicController
2
+ module InstanceMethods
3
+
4
+ def self.included(base)
5
+ base.rescue_from StandardError, with: :handle_error
6
+
7
+ if base.include_action?(:index)
8
+ base.send :define_method, :index do
9
+ if parent_model
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(ransack_query).result(distinct: true).page(params[:page])
20
+ self.collection.all #Force load to handle exception here
21
+ rescue ActiveRecord::StatementInvalid => ex
22
+ Rails.logger.debug "Invalid search query: #{params[:q]} | Error: #{ex.message}"
23
+ self.collection = empty_collection
24
+ end
25
+ else
26
+ self.collection = empty_collection
27
+ end
28
+
29
+ Responder.new(self).index
30
+ end
31
+ end
32
+
33
+ if base.include_action?(:show)
34
+ base.send :define_method, :show do
35
+ if parent_model
36
+ self.model = parent_model.send(controller_name).find(params[:id])
37
+ else
38
+ self.model = resource_class.find(params[:id])
39
+ end
40
+
41
+ Responder.new(self).show
42
+ end
43
+ end
44
+
45
+ if base.include_action?(:new)
46
+ base.send :define_method, :new do
47
+ if parent_model
48
+ self.model = parent_model.send(controller_name).build
49
+ else
50
+ self.model = resource_class.new
51
+ end
52
+
53
+ Responder.new(self).new
54
+ end
55
+ end
56
+
57
+ if base.include_action?(:edit)
58
+ base.send :define_method, :edit do
59
+ if parent_model
60
+ self.model = parent_model.send(controller_name).find(params[:id])
61
+ else
62
+ self.model = resource_class.find(params[:id])
63
+ end
64
+
65
+ Responder.new(self).edit
66
+ end
67
+ end
68
+
69
+ if base.include_action?(:create)
70
+ base.send :define_method, :create do
71
+ if parent_model
72
+ self.model = parent_model.send(controller_name).build(params[controller_name.singularize])
73
+ else
74
+ self.model = resource_class.new(params[controller_name.singularize])
75
+ end
76
+
77
+ if model.save
78
+ flash_message = "#{resource_class.model_name.human} successfully created"
79
+ if params[:save_and_new]
80
+ flash[:success] = flash_message
81
+ redirect_to action: :new
82
+ else
83
+ flash.now[:success] = flash_message
84
+ Responder.new(self).create
85
+ end
86
+ else
87
+ respond_to do |format|
88
+ format.html { render :new }
89
+ format.json { render json: model.errors, status: :unprocessable_entity }
90
+ end
91
+ end
92
+ end
93
+ end
94
+
95
+ if base.include_action?(:update)
96
+ base.send :define_method, :update do
97
+ if parent_model
98
+ self.model = parent_model.send(controller_name).find(params[:id])
99
+ else
100
+ self.model = resource_class.find(params[:id])
101
+ end
102
+
103
+ if model.update_attributes(params[controller_name.singularize])
104
+ flash.now[:success] = "#{resource_class.model_name.human} successfully updated"
105
+ Responder.new(self).update
106
+ else
107
+ respond_to do |format|
108
+ format.html { render :edit }
109
+ format.json { render json: model.errors, status: :unprocessable_entity }
110
+ end
111
+ end
112
+ end
113
+
114
+ end
115
+
116
+ if base.include_action?(:destroy)
117
+ base.send :define_method, :destroy do
118
+ if parent_model
119
+ self.model = parent_model.send(controller_name).find(params[:id])
120
+ else
121
+ self.model = resource_class.find(params[:id])
122
+ end
123
+
124
+ if model.destroy
125
+ flash[:warning] = "#{resource_class.model_name.human} successfully removed"
126
+ Responder.new(self).destroy
127
+ else
128
+ flash[:danger] = "#{resource_class.model_name.human} could not be deleted"
129
+ respond_to do |format|
130
+ format.html { redirect_to action: :index }
131
+ format.json { render json: model.errors, status: :unprocessable_entity }
132
+ end
133
+ end
134
+ end
135
+ end
136
+
137
+ end
138
+
139
+ end
140
140
  end
@@ -1,21 +1,21 @@
1
- module DynamicController
2
- class Resource
3
- attr_reader :resource_class,
4
- :param_name,
5
- :instance_variable_name,
6
- :children_name
7
-
8
- def initialize(options={})
9
- raise 'Param resource_class must be a class' if !options.has_key?(:resource_class) || !options[:resource_class].is_a?(Class)
10
- @resource_class = options[:resource_class]
11
- @param_name = options[:param_name] || "#{resource_class.to_s.demodulize.underscore}_id"
12
- @instance_variable_name = options[:instance_variable_name] || "@#{resource_class.to_s.demodulize.underscore}"
13
- @children_name = options[:children_name] || resource_class.to_s.demodulize.pluralize.underscore
14
- end
15
-
16
- def find(id)
17
- resource_class.find(id)
18
- end
19
-
20
- end
21
- end
1
+ module DynamicController
2
+ class Resource
3
+ attr_reader :resource_class,
4
+ :param_name,
5
+ :instance_variable_name,
6
+ :children_name
7
+
8
+ def initialize(options={})
9
+ raise 'Param resource_class must be a class' if !options.has_key?(:resource_class) || !options[:resource_class].is_a?(Class)
10
+ @resource_class = options[:resource_class]
11
+ @param_name = options[:param_name] || "#{resource_class.to_s.demodulize.underscore}_id"
12
+ @instance_variable_name = options[:instance_variable_name] || "@#{resource_class.to_s.demodulize.underscore}"
13
+ @children_name = options[:children_name] || resource_class.to_s.demodulize.pluralize.underscore
14
+ end
15
+
16
+ def find(id)
17
+ resource_class.find(id)
18
+ end
19
+
20
+ end
21
+ end
@@ -1,71 +1,71 @@
1
- module DynamicController
2
- class Responder
3
-
4
- def initialize(controller)
5
- @controller = controller
6
- end
7
-
8
- def index
9
- action :index,
10
- html: Proc.new {},
11
- json: Proc.new { render json: collection }
12
- end
13
-
14
- def show
15
- action :show,
16
- html: Proc.new {},
17
- json: Proc.new { render json: model }
18
- end
19
-
20
- def new
21
- action :new,
22
- html: Proc.new {}
23
- end
24
-
25
- def edit
26
- action :edit,
27
- html: Proc.new {}
28
- end
29
-
30
- def create
31
- action :create,
32
- html: Proc.new { redirect_to action: :show, id: model.id },
33
- json: Proc.new { render json: model, status: :created }
34
- end
35
-
36
- def update
37
- action :update,
38
- html: Proc.new { redirect_to action: :show, id: model.id },
39
- json: Proc.new { head :no_content }
40
- end
41
-
42
- def destroy
43
- action :destroy,
44
- html: Proc.new { redirect_to action: :index },
45
- json: Proc.new { head :no_content }
46
- end
47
-
48
- private
49
-
50
- def action(name, blocks={})
51
- @controller.instance_eval do
52
- if self.class.redefined_responder_to?(name)
53
- respond_to do |format|
54
- self.instance_exec format, &self.class.redefined_responder_to(name)
55
- end
56
- else
57
- respond_to do |format|
58
- self.class.responder_formats.each do |mime|
59
- if self.class.redefined_responder_to?(name, mime)
60
- format.send(mime) { self.instance_eval &self.class.redefined_responder_to(name, mime) }
61
- elsif blocks[mime]
62
- format.send(mime) { self.instance_eval &blocks[mime] }
63
- end
64
- end
65
- end
66
- end
67
- end
68
- end
69
-
70
- end
1
+ module DynamicController
2
+ class Responder
3
+
4
+ def initialize(controller)
5
+ @controller = controller
6
+ end
7
+
8
+ def index
9
+ action :index,
10
+ html: Proc.new {},
11
+ json: Proc.new { render json: collection }
12
+ end
13
+
14
+ def show
15
+ action :show,
16
+ html: Proc.new {},
17
+ json: Proc.new { render json: model }
18
+ end
19
+
20
+ def new
21
+ action :new,
22
+ html: Proc.new {}
23
+ end
24
+
25
+ def edit
26
+ action :edit,
27
+ html: Proc.new {}
28
+ end
29
+
30
+ def create
31
+ action :create,
32
+ html: Proc.new { redirect_to action: :show, id: model.id },
33
+ json: Proc.new { render json: model, status: :created }
34
+ end
35
+
36
+ def update
37
+ action :update,
38
+ html: Proc.new { redirect_to action: :show, id: model.id },
39
+ json: Proc.new { head :no_content }
40
+ end
41
+
42
+ def destroy
43
+ action :destroy,
44
+ html: Proc.new { redirect_to action: :index },
45
+ json: Proc.new { head :no_content }
46
+ end
47
+
48
+ private
49
+
50
+ def action(name, blocks={})
51
+ @controller.instance_eval do
52
+ if self.class.redefined_responder_to?(name)
53
+ respond_to do |format|
54
+ self.instance_exec format, &self.class.redefined_responder_to(name)
55
+ end
56
+ else
57
+ respond_to do |format|
58
+ self.class.responder_formats.each do |mime|
59
+ if self.class.redefined_responder_to?(name, mime)
60
+ format.send(mime) { self.instance_eval &self.class.redefined_responder_to(name, mime) }
61
+ elsif blocks[mime]
62
+ format.send(mime) { self.instance_eval &blocks[mime] }
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+
70
+ end
71
71
  end
@@ -1,3 +1,3 @@
1
- module DynamicController
2
- VERSION = '0.0.8'
3
- end
1
+ module DynamicController
2
+ VERSION = '0.0.9'
3
+ end
@@ -1,18 +1,18 @@
1
- require 'ransack'
2
- require 'kaminari'
3
- require 'nql'
4
-
5
- module DynamicController
6
- ACTIONS = [:index, :show, :new, :create, :edit, :update, :destroy]
7
- end
8
-
9
- require 'dynamic_controller/version'
10
- require 'dynamic_controller/resource'
11
- require 'dynamic_controller/responder'
12
- require 'dynamic_controller/helper_methods'
13
- require 'dynamic_controller/class_methods'
14
- require 'dynamic_controller/instance_methods'
15
- require 'dynamic_controller/action_controller_extension'
16
-
17
- ActionController::Base.send :extend, DynamicController::ActionControllerExtension
18
-
1
+ require 'ransack'
2
+ require 'kaminari'
3
+ require 'nql'
4
+
5
+ module DynamicController
6
+ ACTIONS = [:index, :show, :new, :create, :edit, :update, :destroy]
7
+ end
8
+
9
+ require 'dynamic_controller/version'
10
+ require 'dynamic_controller/resource'
11
+ require 'dynamic_controller/responder'
12
+ require 'dynamic_controller/helper_methods'
13
+ require 'dynamic_controller/class_methods'
14
+ require 'dynamic_controller/instance_methods'
15
+ require 'dynamic_controller/action_controller_extension'
16
+
17
+ ActionController::Base.send :extend, DynamicController::ActionControllerExtension
18
+
@@ -1,23 +1,23 @@
1
- class AllActionsController < ActionController::Base
2
- has_crud_actions
3
- end
4
-
5
- class OnlyIndexController < ActionController::Base
6
- has_crud_actions only: :index
7
- end
8
-
9
- class ExceptIndexController < ActionController::Base
10
- has_crud_actions except: :index
11
- end
12
-
13
- class OnlyAndExceptController < ActionController::Base
14
- has_crud_actions only: [:index, :new, :create, :edit], except: [:edit, :destroy]
15
- end
16
-
17
- class XlsResponderController < ActionController::Base
18
- has_crud_actions
19
-
20
- respond_to_index :xls do
21
- render xls: nil
22
- end
1
+ class AllActionsController < ActionController::Base
2
+ has_crud_actions
3
+ end
4
+
5
+ class OnlyIndexController < ActionController::Base
6
+ has_crud_actions only: :index
7
+ end
8
+
9
+ class ExceptIndexController < ActionController::Base
10
+ has_crud_actions except: :index
11
+ end
12
+
13
+ class OnlyAndExceptController < ActionController::Base
14
+ has_crud_actions only: [:index, :new, :create, :edit], except: [:edit, :destroy]
15
+ end
16
+
17
+ class XlsResponderController < ActionController::Base
18
+ has_crud_actions
19
+
20
+ respond_to_index :xls do
21
+ render xls: nil
22
+ end
23
23
  end