cruddler 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/Gemfile +29 -0
  4. data/Gemfile.lock +141 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.textile +173 -0
  7. data/Rakefile +40 -0
  8. data/cruddler.gemspec +23 -0
  9. data/lib/cruddler.rb +33 -0
  10. data/lib/cruddler/controller.rb +186 -0
  11. data/lib/cruddler/crud_actions.rb +118 -0
  12. data/lib/cruddler/engine.rb +28 -0
  13. data/lib/cruddler/path_helpers.rb +100 -0
  14. data/lib/cruddler/version.rb +26 -0
  15. data/lib/generators/cruddler/install/USAGE +8 -0
  16. data/lib/generators/cruddler/install/install_generator.rb +40 -0
  17. data/lib/generators/cruddler/install/templates/application/edit.html.erb +18 -0
  18. data/lib/generators/cruddler/install/templates/application/index.html.erb +13 -0
  19. data/lib/generators/cruddler/install/templates/application/new.html.erb +17 -0
  20. data/lib/generators/cruddler/install/templates/application/show.html.erb +17 -0
  21. data/lib/generators/cruddler/install/templates/cruddler.yml +21 -0
  22. data/lib/tasks/cruddler_tasks.rake +4 -0
  23. data/script/rails +8 -0
  24. data/test/cruddler_test.rb +7 -0
  25. data/test/dummy/README.rdoc +261 -0
  26. data/test/dummy/Rakefile +7 -0
  27. data/test/dummy/app/assets/javascripts/application.js +15 -0
  28. data/test/dummy/app/assets/javascripts/cats.js +2 -0
  29. data/test/dummy/app/assets/javascripts/dogs.js +2 -0
  30. data/test/dummy/app/assets/javascripts/houses.js +2 -0
  31. data/test/dummy/app/assets/javascripts/parasites.js +2 -0
  32. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  33. data/test/dummy/app/assets/stylesheets/cats.css +4 -0
  34. data/test/dummy/app/assets/stylesheets/dogs.css +4 -0
  35. data/test/dummy/app/assets/stylesheets/houses.css +4 -0
  36. data/test/dummy/app/assets/stylesheets/parasites.css +4 -0
  37. data/test/dummy/app/assets/stylesheets/scaffold.css +56 -0
  38. data/test/dummy/app/controllers/application_controller.rb +3 -0
  39. data/test/dummy/app/controllers/cats_controller.rb +7 -0
  40. data/test/dummy/app/controllers/dogs_controller.rb +11 -0
  41. data/test/dummy/app/controllers/houses_controller.rb +6 -0
  42. data/test/dummy/app/controllers/parasites_controller.rb +8 -0
  43. data/test/dummy/app/helpers/application_helper.rb +2 -0
  44. data/test/dummy/app/helpers/cats_helper.rb +2 -0
  45. data/test/dummy/app/helpers/dogs_helper.rb +2 -0
  46. data/test/dummy/app/helpers/houses_helper.rb +2 -0
  47. data/test/dummy/app/helpers/parasites_helper.rb +2 -0
  48. data/test/dummy/app/mailers/.gitkeep +0 -0
  49. data/test/dummy/app/models/.gitkeep +0 -0
  50. data/test/dummy/app/models/cat.rb +4 -0
  51. data/test/dummy/app/models/dog.rb +3 -0
  52. data/test/dummy/app/models/house.rb +5 -0
  53. data/test/dummy/app/models/parasite.rb +3 -0
  54. data/test/dummy/app/views/application/edit.html.erb +18 -0
  55. data/test/dummy/app/views/application/index.html.erb +13 -0
  56. data/test/dummy/app/views/application/new.html.erb +17 -0
  57. data/test/dummy/app/views/application/show.html.erb +17 -0
  58. data/test/dummy/app/views/cats/_form.html.erb +15 -0
  59. data/test/dummy/app/views/cats/_listing.html.erb +23 -0
  60. data/test/dummy/app/views/cats/_show_cat.html.erb +10 -0
  61. data/test/dummy/app/views/dogs/_form.html.erb +21 -0
  62. data/test/dummy/app/views/dogs/_listing.html.erb +23 -0
  63. data/test/dummy/app/views/dogs/_show_dog.html.erb +10 -0
  64. data/test/dummy/app/views/houses/_form.html.erb +25 -0
  65. data/test/dummy/app/views/houses/_listing.html.erb +23 -0
  66. data/test/dummy/app/views/houses/_show_house.html.erb +11 -0
  67. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  68. data/test/dummy/app/views/parasites/_form.html.erb +25 -0
  69. data/test/dummy/app/views/parasites/_listing.html.erb +27 -0
  70. data/test/dummy/app/views/parasites/_show_parasite.html.erb +19 -0
  71. data/test/dummy/bin/bundle +3 -0
  72. data/test/dummy/bin/rails +4 -0
  73. data/test/dummy/bin/rake +4 -0
  74. data/test/dummy/config.ru +4 -0
  75. data/test/dummy/config/application.rb +23 -0
  76. data/test/dummy/config/boot.rb +4 -0
  77. data/test/dummy/config/database.yml +25 -0
  78. data/test/dummy/config/environment.rb +5 -0
  79. data/test/dummy/config/environments/development.rb +29 -0
  80. data/test/dummy/config/environments/production.rb +80 -0
  81. data/test/dummy/config/environments/test.rb +36 -0
  82. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  83. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  84. data/test/dummy/config/initializers/inflections.rb +16 -0
  85. data/test/dummy/config/initializers/mime_types.rb +5 -0
  86. data/test/dummy/config/initializers/secret_token.rb +12 -0
  87. data/test/dummy/config/initializers/session_store.rb +3 -0
  88. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  89. data/test/dummy/config/locales/en.yml +23 -0
  90. data/test/dummy/config/routes.rb +12 -0
  91. data/test/dummy/db/migrate/20120606105838_create_houses.rb +10 -0
  92. data/test/dummy/db/migrate/20120606110003_create_cats.rb +11 -0
  93. data/test/dummy/db/migrate/20120606110348_create_dogs.rb +13 -0
  94. data/test/dummy/db/migrate/20120606175547_create_parasites.rb +12 -0
  95. data/test/dummy/db/schema.rb +53 -0
  96. data/test/dummy/lib/assets/.gitkeep +0 -0
  97. data/test/dummy/log/.gitkeep +0 -0
  98. data/test/dummy/public/404.html +26 -0
  99. data/test/dummy/public/422.html +26 -0
  100. data/test/dummy/public/500.html +25 -0
  101. data/test/dummy/public/favicon.ico +0 -0
  102. data/test/dummy/script/rails +6 -0
  103. data/test/integration/cruddler_integration_test.rb +253 -0
  104. data/test/integration/navigation_test.rb +10 -0
  105. data/test/support/capybara.rb +29 -0
  106. data/test/test_helper.rb +15 -0
  107. metadata +253 -0
data/lib/cruddler.rb ADDED
@@ -0,0 +1,33 @@
1
+ #--
2
+ # Copyright (c) 2010-2014 Peter Horn metaminded UG
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ require 'rails'
25
+
26
+ module Cruddler
27
+ end
28
+
29
+ require 'cruddler/version'
30
+ require 'cruddler/engine'
31
+ require 'cruddler/crud_actions'
32
+ require 'cruddler/path_helpers'
33
+ require 'cruddler/controller'
@@ -0,0 +1,186 @@
1
+ #--
2
+ # Copyright (c) 2010-2014 Peter Horn metaminded UG
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ module Cruddler::Controller
25
+
26
+ def cruddler(methods,
27
+ klass: nil,
28
+ parameter_name: nil,
29
+ nested: nil,
30
+ nested_as: nil,
31
+ resources_name: nil,
32
+ resource_name: nil,
33
+ path_components: nil,
34
+ stateful_index: nil,
35
+ authorize: nil,
36
+ index_path: nil,
37
+ after_destroy_path: nil,
38
+ after_create_path: nil,
39
+ after_update_path: nil,
40
+ name: nil,
41
+ use_tabulatr: nil,
42
+ permit_params: nil,
43
+ &params_block)
44
+ # get the class that's to be used if it can't be guessed from the controller name
45
+ klass ||= self.to_s.split("::").last.split("Controller").first.singularize.constantize
46
+ klass_name = klass.to_s.tableize
47
+ parameter_name ||= klass.to_s.tableize.singularize.gsub('/', '_')
48
+ nam = "@" + parameter_name
49
+
50
+ # if the resource is nested, get the wrapping resources
51
+ # :nested => :masterclass
52
+ # :nested => [:supermasterclassname, :masterclassname]
53
+ # :nested => {:supermasterclassname => SuperMasterClassName, :masterclassname => MasterClassName}
54
+ nested = nested.presence
55
+ nested = [nested] if nested.is_a?(String) || nested.is_a?(Symbol)
56
+ nested = case nested
57
+ when nil then []
58
+ when Array then Hash[nested.map{|n| [n.to_s, n.to_s.classify.constantize]}]
59
+ when Hash then nested
60
+ else raise "expected :nested Option to get either a list of model-names or a hash name => Class"
61
+ end
62
+ before_filter :cruddler_get_nested if nested
63
+ nested_as ||= nested.to_a.last.try(:first)
64
+
65
+ # which CRUD methods are to be created?
66
+ methods = case methods
67
+ when :all then [:index, :show, :edit, :update, :new, :create, :destroy]
68
+ when :read then [:index, :show]
69
+ when :none then []
70
+ else [*methods].flatten
71
+ end
72
+
73
+ resources_name ||= self.to_s.split("::").last[0..(-11)].tableize
74
+ resource_name ||= resources_name.singularize
75
+ path_components ||= self.to_s.split("::").map(&:underscore)[0..-2]
76
+
77
+ # This module will be included later on
78
+ mod = Module.new
79
+
80
+ if block_given?
81
+ raise "Don't give :permit_params option if block is given" if permit_params
82
+ mod.send :define_method, "#{parameter_name}_params" do
83
+ self.instance_eval &params_block
84
+ end
85
+ # private "#{parameter_name}_params"
86
+ elsif permit_params == :all
87
+ mod.send :define_method, "#{parameter_name}_params" do
88
+ params.required(parameter_name.to_sym).permit!
89
+ end
90
+ elsif permit_params.is_a? Proc
91
+ mod.send :define_method, "#{parameter_name}_params" do
92
+ pp = self.instance_exec(&permit_params)
93
+ puts pp
94
+ params.required(parameter_name.to_sym).permit(pp)
95
+ end
96
+ elsif permit_params
97
+ mod.send :define_method, "#{parameter_name}_params" do
98
+ if(klass.respond_to?(:translated_attrs))
99
+ permit_params = Array(permit_params).flatten
100
+ translatable_attrs = klass.translated_attrs.select{|a| permit_params.include?(a)}
101
+ permit_params += klass.translation_names_for(translatable_attrs)
102
+ end
103
+ params.required(parameter_name.to_sym).permit(permit_params)
104
+ end
105
+ else
106
+ if klass.respond_to? :permitted_attributes
107
+ mod.send :define_method, "#{parameter_name}_params" do
108
+ params.required(parameter_name.to_sym).permit(klass.permitted_attributes)
109
+ end
110
+ end
111
+ end
112
+
113
+ mod.send :define_method, :cruddler_params do
114
+ if self.respond_to? "#{parameter_name}_params"
115
+ self.send "#{parameter_name}_params"
116
+ else
117
+ raise "Either give a block to cruddler, the :permit_params option, add `permitted_attributes` in the model, or implement method `#{parameter_name}_params`."
118
+ end
119
+ end
120
+ # private :cruddler_params
121
+
122
+ mod.send :define_method, :cruddler do
123
+ @_cruddler ||= OpenStruct.new(
124
+ model_name: nam,
125
+ klass: klass,
126
+ klass_name: klass_name,
127
+ nested: nested,
128
+ nested_as: nested_as,
129
+ parameter_name: parameter_name,
130
+ resource_name: resource_name,
131
+ resources_name: resources_name,
132
+ stateful_index: stateful_index,
133
+ authorize: authorize,
134
+ index_path: index_path,
135
+ after_destroy_path: after_destroy_path,
136
+ after_create_path: after_create_path,
137
+ after_update_path: after_update_path,
138
+ name: name,
139
+ use_tabulatr: use_tabulatr
140
+ )
141
+ end
142
+
143
+ mod.send :define_method, :cruddler_find_on do
144
+ if !nested.present? then klass
145
+ else
146
+ (cruddler_get_nested.last.send(klass_name.pluralize) rescue klass)
147
+ end
148
+ end
149
+
150
+ mod.send :define_method, :resource_name do cruddler.resource_name end
151
+ mod.send :define_method, :resources_name do cruddler.resources_name end
152
+ mod.send :define_method, :cruddler_get_nested do
153
+ nested.map do |nam, nklaz|
154
+ return nil unless params["#{nam}_id"]
155
+ instance_variable_get("@#{nam}") ||
156
+ instance_variable_set("@#{nam}", nklaz.find(params["#{nam}_id"]))
157
+ end
158
+ end
159
+
160
+ # helper
161
+ mod.send :define_method, :current_object do
162
+ instance_variable_get(nam)
163
+ end
164
+ mod.send :alias_method, :cruddler_current_object, :current_object
165
+
166
+ mod.send :define_method, :current_path_components do |*args|
167
+ [path_components, cruddler_get_nested, args].flatten.compact
168
+ end
169
+
170
+ self.send :include, Cruddler::PathHelpers
171
+ self.send :include, mod
172
+
173
+ # inlude the desired CRUD actions. There's always just one method per module
174
+ %w{index show edit update new create destroy}.each do |method|
175
+ next unless methods.member? method.to_sym
176
+ include "Cruddler::CrudActions::#{method.capitalize}".constantize
177
+ end
178
+
179
+ helper_method :resource_name, :resources_name,
180
+ :current_index_path, :current_show_path, :current_new_path,
181
+ :current_edit_path, :cruddler,
182
+ :locale_key, :name_for, :current_path_components, :current_name
183
+ end
184
+ end
185
+
186
+ ActionController::Base.send :extend, Cruddler::Controller
@@ -0,0 +1,118 @@
1
+ #--
2
+ # Copyright (c) 2010-2014 Peter Horn metaminded UG
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ module Cruddler::CrudActions
25
+
26
+ module Index
27
+ def index
28
+ if cruddler.authorize
29
+ authorize! :read, cruddler.klass.new
30
+ end
31
+ models = if cruddler.use_tabulatr != false && cruddler_find_on.respond_to?(:tabulatr)
32
+ tabulatr_for cruddler_find_on
33
+ else
34
+ cruddler_find_on.all
35
+ end
36
+ instance_variable_set(cruddler.model_name.pluralize, models)
37
+ end
38
+ end
39
+
40
+ module Show
41
+ def show
42
+ m = cruddler_find_on.find(params[:id])
43
+ authorize!(:read, m) if cruddler.authorize
44
+ instance_variable_set(cruddler.model_name, m)
45
+ end
46
+ end
47
+
48
+ module Edit
49
+ def edit
50
+ m = cruddler_find_on.find(params[:id])
51
+ authorize!(:update, m) if cruddler.authorize
52
+ instance_variable_set(cruddler.model_name, m)
53
+ end
54
+ end
55
+
56
+ module Update
57
+ def update
58
+ t = cruddler_find_on.find(params[:id])
59
+ instance_variable_set(cruddler.model_name, t)
60
+ if cruddler.authorize && !can?(:update, t)
61
+ flash[:notice] = t(locale_key("authorization_problem"))
62
+ return render(:edit)
63
+ end
64
+ success = t.update_attributes(cruddler_params)
65
+ if success
66
+ flash[:notice] = t(locale_key("update_success"))
67
+ redirect_to after_update_path()
68
+ else
69
+ flash[:alert] = t(locale_key("update_problem"))
70
+ render :edit
71
+ end
72
+ end
73
+ end
74
+
75
+ module New
76
+ def new
77
+ m = cruddler.klass.new
78
+ authorize!(:create, m) if cruddler.authorize
79
+ s = instance_variable_set(cruddler.model_name, m)
80
+ cruddler.nested.to_a.last.try do |name, nklaz|
81
+ m.send("#{cruddler.nested_as}=", instance_variable_get("@#{name}"))
82
+ end
83
+ end
84
+ end
85
+
86
+ module Create
87
+ def create
88
+ t = cruddler.klass.new(cruddler_params)
89
+ instance_variable_set(cruddler.model_name, t)
90
+ if cruddler.authorize && !can?(:create, t)
91
+ flash[:notice] = t(locale_key("authorization_problem"))
92
+ return render(:new)
93
+ end
94
+ cruddler.nested.to_a.last.try do |name, nklaz|
95
+ t.send("#{cruddler.nested_as}=", instance_variable_get("@#{name}"))
96
+ end
97
+ success = t.save
98
+ if success
99
+ flash[:notice] = t(locale_key("create_success"))
100
+ redirect_to after_create_path()
101
+ else
102
+ flash[:alert] = t(locale_key("create_problem"))
103
+ render :new
104
+ end
105
+ end
106
+ end
107
+
108
+ module Destroy
109
+ def destroy
110
+ m = cruddler.klass.find(params[:id])
111
+ authorize!(:destroy, m) if cruddler.authorize
112
+ s = instance_variable_set(cruddler.model_name, m)
113
+ s.destroy
114
+ flash[:notice] = t(locale_key("delete_success"))
115
+ redirect_to after_destroy_path()
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,28 @@
1
+ #--
2
+ # Copyright (c) 2010-2014 Peter Horn metaminded UG
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ module Cruddler
25
+ class Engine < ::Rails::Engine
26
+ isolate_namespace Cruddler
27
+ end
28
+ end
@@ -0,0 +1,100 @@
1
+ #--
2
+ # Copyright (c) 2010-2014 Peter Horn metaminded UG
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ module Cruddler::PathHelpers
25
+
26
+ def current_index_path
27
+ cruddler_path_from(cruddler.index_path) || if cruddler.nested.present?
28
+ edit_polymorphic_path(current_path_components())
29
+ else
30
+ polymorphic_path(current_path_components(cruddler.resources_name))
31
+ end
32
+ end
33
+
34
+ def current_show_path(obj=nil)
35
+ obj ||= cruddler_current_object
36
+ polymorphic_path(current_path_components(obj))
37
+ end
38
+
39
+ def current_edit_path(obj=nil)
40
+ obj ||= cruddler_current_object
41
+ edit_polymorphic_path(current_path_components(obj))
42
+ end
43
+
44
+ def current_new_path
45
+ new_polymorphic_path(current_path_components(cruddler.resource_name))
46
+ end
47
+
48
+ def after_update_path
49
+ cruddler_path_from(cruddler.after_update_path) || current_index_path()
50
+ end
51
+
52
+ def after_create_path
53
+ cruddler_path_from(cruddler.after_create_path) || current_index_path()
54
+ end
55
+
56
+ def after_destroy_path
57
+ cruddler_path_from(cruddler.after_destroy_path) || current_index_path()
58
+ end
59
+
60
+ def locale_key(str)
61
+ (['cruddler'] + current_path_components(cruddler.resource_name,str)).map{|c|
62
+ c.is_a?(String) || c.is_a?(Symbol) ? c : c.class.to_s.underscore
63
+ }.join(".")
64
+ end
65
+
66
+ def name_for(record)
67
+ if cruddler.name then record.send cruddler.name
68
+ elsif record.respond_to?(:name) then record.name
69
+ elsif record.respond_to?(:title) then record.title
70
+ else "**unknown**"
71
+ end
72
+ end
73
+
74
+ def current_name
75
+ return nil unless current_object
76
+ name_for(current_object)
77
+ end
78
+
79
+ def cruddler_path_from(s)
80
+ obj = instance_variable_get(cruddler.model_name)
81
+ case s
82
+ when nil, false then nil
83
+ when String then s
84
+ when :index, 'index' then current_index_path()
85
+ when :show, 'show' then current_show_path(obj)
86
+ when :edit, 'edit' then current_edit_path(obj)
87
+ when :new, 'new' then current_new_path()
88
+ when Proc
89
+ if s.arity == 0
90
+ instance_exec(&s)
91
+ elsif s.arity == 1
92
+ instance_exec(obj, &s)
93
+ else
94
+ raise "Don't know how to deliver more than one parameter."
95
+ end
96
+ when "moo" then raise "haha, sehr witzig"
97
+ else raise "Don't know how to deal with `#{s}`."
98
+ end
99
+ end
100
+ end