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
@@ -0,0 +1,46 @@
1
+ module Resources
2
+ class Configuration
3
+
4
+ def initialize *args
5
+ options = args.extract_options!
6
+ self.rest_actions = options[:rest_actions] || true
7
+ self.search = options[:search] || false
8
+ self.search_options = options[:search_options] && options[:search_options].is_a?(Hash) ? options[:search_options] : {distinct: false}
9
+ self.params_search = options[:params_search] || :q
10
+ self.params_resource = options[:params_resource] || :resource
11
+ self.params_page = options[:params_page] || :page
12
+ self.pagination = options[:pagination] || false
13
+ self.resource_scope = options[:resource_scope] || nil
14
+ self.resources_scope = options[:resources_scope] || nil
15
+ self.resource_method_name = options[:resource_method_name] || :resource
16
+ self.resources_method_name = options[:resources_method_name] || nil
17
+ if self.resource_method_name && self.resources_method_name.to_s.blank? && self.resource_method_name.to_s != "resource"
18
+ self.resources_method_name = self.resource_method_name.to_s.pluralize
19
+ end
20
+ end
21
+
22
+
23
+ def to_hash
24
+ hash =HashWithIndifferentAccess.new
25
+ self.class.accessors.each do |m|
26
+ hash[m] = send(m)
27
+ end
28
+ hash
29
+ end
30
+
31
+ def self.accessors
32
+ [:rest_actions, :search, :search_options, :params_search, :resource_class_name, :resource_scope, :resources_scope, :resource_method_name, :resources_method_name, :params_resource, :pagination, :params_page]
33
+ end
34
+ accessors.map{|a| attr_accessor a }
35
+
36
+
37
+ end
38
+
39
+ Config = Resources::Configuration.new()
40
+
41
+ def self.config(&block)
42
+ yield(Config)
43
+ end
44
+
45
+
46
+ end
@@ -0,0 +1,25 @@
1
+ module Resources
2
+ module Controller
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ end
7
+
8
+ module ClassMethods
9
+
10
+ def resource_for name = nil, *args
11
+ options = Resources::Config.to_hash.deep_merge(args.extract_options!)
12
+ @resource_configuration = Resources::Configuration.new(options)
13
+ @resource_configuration.resource_class_name = name
14
+ include Resources::Actions
15
+ include Resources::RestActions if @resource_configuration.rest_actions
16
+ end
17
+
18
+ def resource_configuration
19
+ @resource_configuration
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,109 @@
1
+ module Resources
2
+ class Manager
3
+ include Resources::Routes
4
+
5
+ def initialize controller, request, *args
6
+ @controller = controller
7
+ @request = request
8
+ @settings = @controller.class.resource_configuration
9
+ end
10
+
11
+ def resource_class
12
+ @resource_class_name ||= settings.resource_class_name.to_s.safe_constantize
13
+ end
14
+
15
+ def resources_scope
16
+ scope = option_with_scope(:resources_scope)
17
+ @resources_scope =
18
+ case
19
+ when pagination?
20
+ scope = scope.page(params_page) if scope.respond_to?(:page)
21
+ scope = scope.paginate(page: params_page) if scope.respond_to?(:paginate)
22
+ scope
23
+ else
24
+ scope
25
+ end
26
+ end
27
+
28
+ def pagination?
29
+ settings.pagination.is_a?(Proc) ? settings.pagination.call(params, controller) : settings.pagination
30
+ end
31
+
32
+ def resource_scope
33
+ @resource_scope = option_with_scope(:resource_scope)
34
+ end
35
+
36
+ def resources_search
37
+ @resources_search ||= resources_scope.search(params_search) rescue nil
38
+ end
39
+
40
+
41
+ def resources
42
+ @resources ||= settings.search ? resources_search.result(settings.search_options) : resources_scope
43
+ end
44
+
45
+ def resource
46
+ @resource ||=
47
+ case controller.action_name
48
+ when "new", "create"
49
+ build_resource
50
+ else
51
+ resource_scope.where("#{resource_class.table_name}.id = ?",params[:id]).first rescue nil
52
+ end
53
+ end
54
+
55
+ def settings
56
+ @settings
57
+ end
58
+
59
+ def params
60
+ request.params
61
+ end
62
+
63
+ def params_search
64
+ @params_search ||= option_with_params(:params_search)
65
+ end
66
+
67
+ def params_page
68
+ @params_page ||= option_with_params(:params_page)
69
+ end
70
+
71
+ def params_resource
72
+ @params_resource ||= option_with_params(:params_resource)
73
+ end
74
+
75
+ def build_resource
76
+ resource_class.new(params_resource)
77
+ end
78
+
79
+
80
+
81
+ private
82
+
83
+
84
+ def option_with_scope name
85
+ scope = Rails::VERSION::MAJOR >= 4 ? resource_class.all : resource_class.scoped
86
+ if settings.send(name)
87
+ settings.send(name).is_a?(Proc) ? settings.send(name).call(scope,params,controller) : scope.send(settings.send(name))
88
+ else
89
+ scope
90
+ end
91
+ end
92
+
93
+ def option_with_params name
94
+ settings.send(name).is_a?(Proc) ? settings.send(name).call(params) : params[settings.send(name)]
95
+ end
96
+
97
+ def controller
98
+ @controller
99
+ end
100
+
101
+ def request
102
+ @request
103
+ end
104
+
105
+
106
+
107
+
108
+ end
109
+ end
@@ -0,0 +1,88 @@
1
+ module Resources
2
+ module RestActions
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ respond_to :html, :js, :json
7
+ helper_method :resource_saved?
8
+ if self.resource_configuration.resource_method_name
9
+ self.class_eval <<-RUBY
10
+ alias_method :save_#{self.resource_configuration.resource_method_name.to_s}, :save_resource
11
+ alias_method :destroy_#{self.resource_configuration.resource_method_name.to_s}, :destroy_resource
12
+ alias_method :#{self.resource_configuration.resource_method_name.to_s}_saved?, :resource_saved?
13
+ helper_method :#{self.resource_configuration.resource_method_name.to_s}_saved?
14
+ RUBY
15
+ end
16
+ end
17
+
18
+ def index
19
+
20
+ end
21
+
22
+ def new
23
+
24
+ end
25
+
26
+ def create
27
+ save_resource
28
+ end
29
+
30
+ def edit
31
+
32
+ end
33
+
34
+ def update
35
+ save_resource
36
+ end
37
+
38
+ def destroy
39
+ destroy_resource
40
+ end
41
+
42
+ protected
43
+
44
+ def resource_saved?
45
+ @resource_saved
46
+ end
47
+
48
+ def save_resource &block
49
+ resource.attributes = resource_manager.params_resource unless resource.new_record?
50
+ @resource_saved = resource.save
51
+ after_redirect_for = "after_#{action_name}_path_for"
52
+ action_path_for =
53
+ case action_name
54
+ when "create"
55
+ :new
56
+ when "update"
57
+ :edit
58
+ else
59
+ :index
60
+ end
61
+ if block_given?
62
+ block.call(resource)
63
+ else
64
+ if self.respond_to?(after_redirect_for, true)
65
+ respond_with resource, location: send(after_redirect_for)
66
+ else
67
+ respond_with resource, location: url_for(controller: params[:controller], action: :index), action: action_path_for
68
+ end
69
+ end
70
+ end
71
+
72
+ def destroy_resource &block
73
+ @destroy_resource = resource.destroy
74
+ after_redirect_for = "after_#{action_name}_path_for"
75
+ if block_given?
76
+ block.call(@destroy_resource)
77
+ else
78
+ if self.respond_to?(after_redirect_for, true)
79
+ respond_with resource, location: send(after_redirect_for), action: :destroy
80
+ else
81
+ respond_with resource, location: url_for(controller: params[:controller], action: :index), action: :destroy
82
+ end
83
+ end
84
+ end
85
+
86
+
87
+ end
88
+ end
@@ -0,0 +1,47 @@
1
+ module Resources
2
+ module Routes
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ end
7
+
8
+ def routes
9
+ Rails.application.routes || request.env['action_dispatch.routes']
10
+ end
11
+
12
+ def named_routes
13
+ routes.named_routes
14
+ end
15
+
16
+ def router
17
+ routes.router
18
+ end
19
+
20
+ def path_parameters
21
+ request.env['action_dispatch.request.path_parameters'].symbolize_keys
22
+ end
23
+
24
+ def current_route
25
+ recognize_route(request)
26
+ end
27
+
28
+ def current_path
29
+ recognize_path(request.path)
30
+ end
31
+
32
+
33
+
34
+ protected
35
+
36
+ def recognize_path(path)
37
+ routes.recognize_path(path)
38
+ end
39
+
40
+ def recognize_route(request)
41
+ router.recognize(request) do |route, matches, params|
42
+ return route
43
+ end
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ module Resources
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :resources do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
6
+ <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run