resources 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +20 -0
  3. data/README.md +230 -0
  4. data/Rakefile +34 -0
  5. data/lib/generators/rails/USAGE +25 -0
  6. data/lib/generators/rails/resource_controller_generator.rb +13 -0
  7. data/lib/generators/rails/templates/controller.rb +9 -0
  8. data/lib/generators/resources/install/USAGE +10 -0
  9. data/lib/generators/resources/install/install_generator.rb +8 -0
  10. data/lib/generators/resources/install/templates/config/initializers/resources.rb +30 -0
  11. data/lib/loader.rb +6 -0
  12. data/lib/resources.rb +5 -0
  13. data/lib/resources/actions.rb +46 -0
  14. data/lib/resources/configuration.rb +46 -0
  15. data/lib/resources/controller.rb +25 -0
  16. data/lib/resources/manager.rb +109 -0
  17. data/lib/resources/rest_actions.rb +88 -0
  18. data/lib/resources/routes.rb +47 -0
  19. data/lib/resources/version.rb +3 -0
  20. data/lib/tasks/resources_tasks.rake +4 -0
  21. data/test/dummy/README.rdoc +28 -0
  22. data/test/dummy/Rakefile +6 -0
  23. data/test/dummy/app/assets/javascripts/application.js +13 -0
  24. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  25. data/test/dummy/app/controllers/application_controller.rb +5 -0
  26. data/test/dummy/app/helpers/application_helper.rb +2 -0
  27. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  28. data/test/dummy/bin/bundle +3 -0
  29. data/test/dummy/bin/rails +4 -0
  30. data/test/dummy/bin/rake +4 -0
  31. data/test/dummy/bin/setup +29 -0
  32. data/test/dummy/config.ru +4 -0
  33. data/test/dummy/config/application.rb +26 -0
  34. data/test/dummy/config/boot.rb +5 -0
  35. data/test/dummy/config/database.yml +25 -0
  36. data/test/dummy/config/environment.rb +5 -0
  37. data/test/dummy/config/environments/development.rb +41 -0
  38. data/test/dummy/config/environments/production.rb +79 -0
  39. data/test/dummy/config/environments/test.rb +42 -0
  40. data/test/dummy/config/initializers/assets.rb +11 -0
  41. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  42. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  43. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  44. data/test/dummy/config/initializers/inflections.rb +16 -0
  45. data/test/dummy/config/initializers/mime_types.rb +4 -0
  46. data/test/dummy/config/initializers/session_store.rb +3 -0
  47. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  48. data/test/dummy/config/locales/en.yml +23 -0
  49. data/test/dummy/config/routes.rb +56 -0
  50. data/test/dummy/config/secrets.yml +22 -0
  51. data/test/dummy/public/404.html +67 -0
  52. data/test/dummy/public/422.html +67 -0
  53. data/test/dummy/public/500.html +66 -0
  54. data/test/dummy/public/favicon.ico +0 -0
  55. data/test/resources_test.rb +7 -0
  56. data/test/test_helper.rb +18 -0
  57. metadata +139 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 36863daefb238f15a3d9f0ad1a9ecd59e1cbdf30
4
+ data.tar.gz: face9855059f06f5321be5188c3141c95903424c
5
+ SHA512:
6
+ metadata.gz: 2eb85882c8de98dd5a1b0e34f41e74b7609d7465ed502c482980c5081f4f142c477825a58ad85fcc78fcf656417f7797a3aa7523a9a272d835d988f4322314fe
7
+ data.tar.gz: f56fac3ea48fe486a18b07a39b0dcaac534e393cdbc348d0e7e710dcd2b18acf1cb910d60aae9a37c167df035b4c6f07532a899197e6754c853698fbab05c92a
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Emilio Forrer
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,230 @@
1
+ **Resources**
2
+ ===================
3
+
4
+ It's a gem that help's you to create **CRUD** controllers for your models in a **very fast** and **flexible** way.
5
+
6
+ ----------
7
+
8
+
9
+ **Installation**
10
+ ------------
11
+
12
+
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```
17
+ gem 'resources'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ ```
23
+ bundle install
24
+ ```
25
+
26
+ Or install it yourself as:
27
+
28
+ ```
29
+ $ gem install resources
30
+ ```
31
+
32
+ Rub the configuration generator, this will create the **resources initializer**
33
+
34
+ ```
35
+ rails g resources:install
36
+ ```
37
+
38
+ ----------
39
+
40
+ USAGE
41
+ -------------
42
+
43
+ Just add `resource_for` to your controller and give the model's name
44
+
45
+ ```
46
+ class Admin::CountriesController < ApplicationController
47
+ resource_for "Country"
48
+ end
49
+ ```
50
+
51
+ or use the controller generator
52
+
53
+ ```
54
+ rails g resource_controller admin/country
55
+ ```
56
+
57
+ And that's it, you have to do anything else on the controller to create a **CRUD** maintenance for the country model.
58
+
59
+ `resource_for` will **handle all the restful** actions for you and it will give you the next's helper_methods for your convenience:
60
+
61
+ **`resources`:** this will have a collection of `Country`, objects so you can use on the `index` actions or any other collection route actions of your controller
62
+
63
+ **`resource`:** this will have a `Country`, object so you can use on the member route actions like `new, create, edit, update, show, destroy`
64
+
65
+ **`resources_search`:** this is a **[ransack](https://github.com/activerecord-hackery/ransack)** object to you in the `search_form_for` helper
66
+
67
+ **`resource_saved?`:** this is a helper to user after create or update a record to know if it was saved.
68
+
69
+
70
+ ## OPTIONS AND FLEXIBILITY
71
+
72
+
73
+
74
+ `resource_for` by default expects the `params` key for update or create the object to be `resource` if you use `form_for` or `simple_form` you need to add the `as: :resource` option
75
+
76
+
77
+ ```
78
+ = simple_form_for [:admin, resource], as: :resource do |f|
79
+ = f.input :code
80
+ = f.input :name
81
+ = f.input :active
82
+ = f.submit "SAVE"
83
+ ```
84
+
85
+
86
+
87
+ #### **RESOURCE PARAMS - `params_resource`**
88
+
89
+ This options allows you to change the default params key used for create or update the record. So you have to add the `as: :resource` to the `simple_form_for`
90
+
91
+ ```
92
+ class Admin::CountriesController < ApplicationController
93
+ resource_for :"Country",
94
+ params_resource: :country
95
+ end
96
+ ```
97
+
98
+ Or you can use the `lambda` syntax.
99
+
100
+ ```
101
+ class Admin::CountriesController < ApplicationController
102
+ resource_for :"Country",
103
+ params_resource: lambda{ |params| params[:country] }
104
+ end
105
+ ```
106
+
107
+ #### **RESOURCE ALIAS - `resource_method_name` - `resources_method_name`**
108
+
109
+ This options allows you to alias the `resource` method in case you don't like the name. if you don't specify a `resources_method_name` it will name the alias to `resource_method_name` **pluralized**.
110
+
111
+ ```
112
+ class Admin::CountriesController < ApplicationController
113
+ resource_for :"Country",
114
+ params_resource: :country,
115
+ resource_method_name: :country
116
+ end
117
+ ```
118
+ **NOTE:** making a change in `resource_method_name` and `resources_method_name` options requires a **server restart**.
119
+
120
+ Now in your view you can use `country` instead of `resource`
121
+
122
+ ```
123
+ = simple_form_for [:admin, country] do |f|
124
+ = f.input :code
125
+ = f.input :name
126
+ = f.input :active
127
+ = f.submit "SAVE"
128
+ ```
129
+
130
+
131
+ #### **PAGINATION - `pagination`**
132
+
133
+ This option enable pagination in case that you use any of the most popular pagination gems ([kaminari](https://github.com/amatsuda/kaminari) or [will_paginate](https://github.com/mislav/will_paginate)).
134
+
135
+ ```
136
+ class Admin::CountriesController < ApplicationController
137
+ resource_for :"Country",
138
+ pagination: true,
139
+ params_resource: :country,
140
+ resource_method_name: :country
141
+ end
142
+ ```
143
+
144
+ Or you can use the `lambda` syntax.
145
+
146
+ ```
147
+ class Admin::CountriesController < ApplicationController
148
+ resource_for :"Country",
149
+ pagination: lambda{ |params, controller| params[:disable_pagination].blank? },
150
+ params_resource: :country,
151
+ resource_method_name: :country
152
+ end
153
+ ```
154
+
155
+ #### **DEFAULT SCOPES - `resource_scope`- `resources_scope`**
156
+
157
+ This options are to set a default `scope` for the `resources`, and `resource` objects.
158
+
159
+
160
+ ```
161
+ class Admin::CountriesController < ApplicationController
162
+ resource_for :"Country",
163
+ resources_scope: :active,
164
+ pagination: true,
165
+ params_resource: :country,
166
+ resource_method_name: :country
167
+ end
168
+ ```
169
+ **Note:** this will execute the scope `active` and get you only the countries that are currently actives.
170
+
171
+ You can use the `lambda` syntax for a **more complex scope**.
172
+
173
+ ```
174
+ class Admin::CountriesController < ApplicationController
175
+ resource_for :"Country",
176
+ resources_scope: lambda{ |scope, params, controller|
177
+ scope.by_active_state(params[:active_state]).includes(:cities)
178
+ },
179
+ pagination: true,
180
+ params_resource: :country,
181
+ resource_method_name: :country
182
+ end
183
+ ```
184
+
185
+
186
+ #### **SEARCH - `search`- `search_options`**
187
+
188
+ The `search` option enable [ransack](https://github.com/activerecord-hackery/ransack) in case that you have installed it. And the `search_options` allows you to pass a hash of parameters to the result method of ransack like **`distinct: true`**
189
+
190
+
191
+ ```
192
+ class Admin::CountriesController < ApplicationController
193
+ resource_for :"Country",
194
+ search: true,
195
+ search_options: {distinct: true},
196
+ resources_scope: :active,
197
+ pagination: true,
198
+ params_resource: :country,
199
+ resource_method_name: :country
200
+ end
201
+ ```
202
+ **NOTE:** For the use of the** `search_form_for`** you have the `resources_search` helper that is a racksack object or in this case `countries_search` because we used the resource alias `resource_method_name`
203
+
204
+ ```
205
+ = search_form_for [:admin, countries_search] do |f|
206
+ = f.label :name_cont
207
+ = f.search_field :name_cont
208
+ = f.submit
209
+ ```
210
+
211
+ #### **GLOBAL CONFIGURATION **
212
+
213
+ You can change the default parameters of any configuration by change the options in the `config/initializers/resources.rb` that was generated by running `rails g resources:install`
214
+
215
+ ```
216
+ Resources.config do |config|
217
+ config.params_search = :q # ransack search parameter. Default params[:q]
218
+ config.params_resource = :resource # resource parameter to be saved.
219
+ end
220
+ ```
221
+
222
+
223
+ ----------
224
+
225
+
226
+ ## **Copyright**
227
+
228
+ Copyright (c) 2015 Emilio Forrer. See LICENSE.txt for further details.
229
+
230
+
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Resources'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,25 @@
1
+ Description:
2
+
3
+ Generate Resource Controller
4
+
5
+ Example:
6
+
7
+ rails g resource_controller country
8
+
9
+ This will create:
10
+ create app/controllers/countries_controller.rb
11
+ create app/views/countries
12
+ create app/views/countries/index.html.erb
13
+ create app/views/countries/edit.html.erb
14
+ create app/views/countries/show.html.erb
15
+ create app/views/countries/new.html.erb
16
+ create app/views/countries/_form.html.erb
17
+ create test/functional/countries_controller_test.rb
18
+ create app/helpers/countries_helper.rb
19
+ create test/unit/helpers/countries_helper_test.rb
20
+
21
+ and inside of the controller it will call resource_for
22
+
23
+ class CountriesController < ApplicationController
24
+ resource_for :"Country"
25
+ end
@@ -0,0 +1,13 @@
1
+ require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
2
+
3
+ module Rails
4
+ module Generators
5
+ class ResourceControllerGenerator < Rails::Generators::ScaffoldControllerGenerator
6
+ def self.source_root
7
+ @source_root ||= File.expand_path('../templates', __FILE__)
8
+ end
9
+ end
10
+ end
11
+ end
12
+
13
+
@@ -0,0 +1,9 @@
1
+ <% if namespaced? -%>
2
+ require_dependency "<%= namespaced_file_path %>/application_controller"
3
+
4
+ <% end -%>
5
+ <% module_namespacing do -%>
6
+ class <%= controller_class_name %>Controller < ApplicationController
7
+ resource_for :"<%= controller_class_name.to_s.singularize.demodulize %>"
8
+ end
9
+ <% end -%>
@@ -0,0 +1,10 @@
1
+ Description:
2
+
3
+ Configuration Initializer
4
+
5
+ Example:
6
+
7
+ rails g resources:install
8
+
9
+ This will create:
10
+ config/initializers/resources.rb
@@ -0,0 +1,8 @@
1
+ class Resources::InstallGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('../templates', __FILE__)
3
+ def generate_install
4
+ copy_file "config/initializers/resources.rb", "config/initializers/resources.rb"
5
+ end
6
+
7
+
8
+ end
@@ -0,0 +1,30 @@
1
+ # GLobal Configuration
2
+ Resources.config do |config|
3
+
4
+ config.rest_actions = true # Enable restful actions (index,new, create, etc)
5
+ config.search = false # Enable ransack search object (resources_search)
6
+ config.search_options = {distinct: false} # ransack result options. Default {distinct: false}
7
+
8
+ # Setting Params (params_search, params_resource, etc)
9
+ # You can specify a params key like :q, that will be params[:q]
10
+ # Or you can set a lambda like lambda{ |params| params[:q] }
11
+ config.params_search = :q # ransack search parameter. Default params[:q]
12
+ config.params_resource = :resource # resource parameter to be saved. Default params[:resource]
13
+ config.params_page = :page # Pagination parameter. Default params[:page]
14
+
15
+
16
+ config.pagination = false # Enables pagination (kaminari or will_paginate)
17
+
18
+ # Setting a default scope for the collection and resource objects
19
+ # You can specify an scope name to be executed like :active
20
+ # Or if you have something more complex or need to pass a parameter to the scope, you can use a lambda like lambda{ |scope, params, controller| scope.by_activation(params[:active]).includes(:states) }
21
+ config.resource_scope = nil # default scope for resource object
22
+ config.resources_scope = nil # default scope for resources collection
23
+
24
+ # Setting alias method of resource, if you change the value to, for example :country you will gain this extra helper methods:
25
+ # countries for the collection object
26
+ # country for the resource object
27
+ # country_saved? to know if the resource was saved
28
+ config.resource_method_name = :resource # alias method of resource
29
+ config.resources_method_name = nil # alias method of resources, if you dont specify one it takes "resource_method_name.to_s.pluralize"
30
+ end
data/lib/loader.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "resources/actions"
2
+ require "resources/rest_actions"
3
+ require "resources/configuration"
4
+ require "resources/controller"
5
+ require "resources/routes"
6
+ require "resources/manager"
data/lib/resources.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "loader"
2
+ module Resources
3
+
4
+ end
5
+ ActionController::Base.send(:include, Resources::Controller)
@@ -0,0 +1,46 @@
1
+ module Resources
2
+ module Actions
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ helper_method :resources, :resource, :resources_search
7
+
8
+ if self.resource_configuration.resource_method_name
9
+ self.class_eval <<-RUBY
10
+
11
+ alias_method :#{self.resource_configuration.resources_method_name.to_s}, :resources
12
+ alias_method :#{self.resource_configuration.resource_method_name.to_s}, :resource
13
+ alias_method :#{self.resource_configuration.resources_method_name.to_s}_search, :resources_search
14
+ helper_method :#{self.resource_configuration.resources_method_name.to_s}, :#{self.resource_configuration.resource_method_name.to_s}, :#{self.resource_configuration.resources_method_name.to_s}_search
15
+ RUBY
16
+ end
17
+ end
18
+
19
+ def resources_search
20
+ @resources_search ||= resource_manager.resources_search
21
+ end
22
+
23
+ def resources
24
+ @resources ||= resource_manager.resources
25
+ end
26
+
27
+ def resource
28
+ @resource ||= resource_manager.resource
29
+ end
30
+
31
+
32
+
33
+ protected
34
+
35
+ def resource_manager
36
+ @resource_manager ||= Resources::Manager.new(self, request)
37
+ end
38
+
39
+
40
+
41
+
42
+ def member_route?
43
+ params[:id].present?
44
+ end
45
+ end
46
+ end