controller_resources 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.md +67 -0
  7. data/Rakefile +2 -0
  8. data/bin/rails +12 -0
  9. data/controller_resources.gemspec +30 -0
  10. data/lib/controller_resources.rb +7 -0
  11. data/lib/controller_resources/engine.rb +12 -0
  12. data/lib/controller_resources/extension.rb +94 -0
  13. data/lib/controller_resources/responder.rb +0 -0
  14. data/lib/controller_resources/version.rb +3 -0
  15. data/lib/generators/controller_resources/USAGE +8 -0
  16. data/lib/generators/controller_resources/install_generator.rb +16 -0
  17. data/lib/tasks/controller_resources_tasks.rake +4 -0
  18. data/spec/controller_resources/extension_spec.rb +32 -0
  19. data/spec/dummy/README.rdoc +28 -0
  20. data/spec/dummy/Rakefile +6 -0
  21. data/spec/dummy/app/assets/images/.keep +0 -0
  22. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  23. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  24. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  25. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  26. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  27. data/spec/dummy/app/mailers/.keep +0 -0
  28. data/spec/dummy/app/models/.keep +0 -0
  29. data/spec/dummy/app/models/concerns/.keep +0 -0
  30. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  31. data/spec/dummy/bin/bundle +3 -0
  32. data/spec/dummy/bin/rails +4 -0
  33. data/spec/dummy/bin/rake +4 -0
  34. data/spec/dummy/config.ru +4 -0
  35. data/spec/dummy/config/application.rb +23 -0
  36. data/spec/dummy/config/boot.rb +5 -0
  37. data/spec/dummy/config/database.yml +85 -0
  38. data/spec/dummy/config/environment.rb +5 -0
  39. data/spec/dummy/config/environments/development.rb +37 -0
  40. data/spec/dummy/config/environments/production.rb +83 -0
  41. data/spec/dummy/config/environments/test.rb +39 -0
  42. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  43. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  44. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  45. data/spec/dummy/config/initializers/inflections.rb +16 -0
  46. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  47. data/spec/dummy/config/initializers/session_store.rb +3 -0
  48. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  49. data/spec/dummy/config/locales/en.yml +23 -0
  50. data/spec/dummy/config/routes.rb +4 -0
  51. data/spec/dummy/config/secrets.yml +22 -0
  52. data/spec/dummy/lib/assets/.keep +0 -0
  53. data/spec/dummy/log/.keep +0 -0
  54. data/spec/dummy/public/404.html +67 -0
  55. data/spec/dummy/public/422.html +67 -0
  56. data/spec/dummy/public/500.html +66 -0
  57. data/spec/dummy/public/favicon.ico +0 -0
  58. data/spec/generators/controller_resources/install_generator_spec.rb +29 -0
  59. data/spec/integration/navigation_spec.rb +4 -0
  60. data/spec/spec_helper.rb +20 -0
  61. metadata +275 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2b490004ef78f9c21c3c72bb3d8de57c7ce52bde
4
+ data.tar.gz: 90ceb34f2312f142bf0a9aeab4d4ff55fc1a9709
5
+ SHA512:
6
+ metadata.gz: d968d9c6ebad11cf03786a310ede7006a45d0277275987cfaf9c0e1083dff79615f02519248d0bea53cc5c6981f0887de44164b5f3529275555c332a92f08fa0
7
+ data.tar.gz: 1d224b6827f81280155ac18f94d485aa86a937ab6c4ea78b4d23d53515e1b0f46c155471e44ef6decbcd16920ab9f4b33607904a25a5eced6ff94b044d2cd9bf
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in controller_resources.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Tom Scott
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 YOURNAME
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,67 @@
1
+ # ControllerResources
2
+
3
+ A Rails engine that unites DecentExposure, StrongParameters, Devise (if
4
+ installed) and Draper (if installed) to provide one hell of an awesome
5
+ controller DSL.
6
+
7
+ ## Features
8
+
9
+ - Provides a common DSL for describing strong parameters as well as
10
+ rules for DecentExposure and response types (default: html,json)
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'controller_resources'
18
+ ```
19
+
20
+ Then run the following generator to generate the locale file:
21
+
22
+ ```bash
23
+ $ rails generate controller_resources:install
24
+ ```
25
+
26
+ ## Usage
27
+
28
+ Define your resource in the controller, and you can use methods instead
29
+ of instance variables to access the model object. No more writing finder
30
+ methods!
31
+
32
+ ```ruby
33
+ class ItemsController < ApplicationController
34
+ resource :item do
35
+ search :name, :user
36
+ modify :name, :user, :is_private
37
+ end
38
+
39
+ def index
40
+ respond_with items
41
+ end
42
+
43
+ def show
44
+ respond_with item
45
+ end
46
+
47
+ def create
48
+ item.save
49
+ respond_with item
50
+ end
51
+ end
52
+ ```
53
+
54
+ In your view, you can use methods instead of instance variables to
55
+ access the model objects passed down into the template:
56
+
57
+ ```erb
58
+ <%= user.name %>
59
+ ```
60
+
61
+ ## Contributing
62
+
63
+ 1. Fork it ( https://github.com/tubbo/controller_resources/fork )
64
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
65
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
66
+ 4. Push to the branch (`git push origin my-new-feature`)
67
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/rails ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 4 gems installed from the root of your application.
3
+
4
+ ENGINE_ROOT = File.expand_path('../..', __FILE__)
5
+ ENGINE_PATH = File.expand_path('../../lib/controller_resources/engine', __FILE__)
6
+
7
+ # Set up gems listed in the Gemfile.
8
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
9
+ require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
10
+
11
+ require 'rails/all'
12
+ require 'rails/engine/commands'
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'controller_resources/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "controller_resources"
8
+ spec.version = ControllerResources::VERSION
9
+ spec.authors = ["Tom Scott"]
10
+ spec.email = ["tubbo@psychedeli.ca"]
11
+ spec.summary = %q{A "glue" for decent_exposure, responders, draper and strong_params}
12
+ spec.description = %q{A "glue" for decent_exposure, responders, draper and strong_params}
13
+ spec.homepage = "https://github.com/tubbo/controller_resources"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^|spec/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3"
24
+ spec.add_development_dependency "pg"
25
+ spec.add_development_dependency "generator_spec"
26
+
27
+ spec.add_dependency "rails", "~> 4.1"
28
+ spec.add_dependency "decent_exposure"
29
+ spec.add_dependency "responders"
30
+ end
@@ -0,0 +1,7 @@
1
+ require "controller_resources/engine"
2
+ require "controller_resources/version"
3
+ require "controller_resources/extension"
4
+
5
+ module ControllerResources
6
+ # config
7
+ end
@@ -0,0 +1,12 @@
1
+ module ControllerResources
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace ControllerResources
4
+ config.to_prepare do
5
+ require 'controller_resources'
6
+
7
+ class ApplicationController < ActionController::Base
8
+ include ControllerResources::Extension
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,94 @@
1
+ require 'decent_exposure'
2
+ require 'responders'
3
+
4
+ module ControllerResources
5
+ # A single macro that combines all controller-level macros we use for
6
+ # the front-end of this application. Simply use the
7
+ # `resource :resource_name` macro in your controller class
8
+ # to make it work.
9
+ #
10
+ # Example:
11
+ #
12
+ # class ArtistsController < ApplicationController
13
+ # resource :artist
14
+ #
15
+ # def index
16
+ # respond_with artists
17
+ # end
18
+ # end
19
+ #
20
+ module Extension
21
+ extend ActiveSupport::Concern
22
+
23
+ included do
24
+ # Attributes for configuring ControllerResources.
25
+ class_attribute :_search_params
26
+ class_attribute :_edit_params
27
+ class_attribute :_singleton_resource
28
+ class_attribute :_collection_resource
29
+
30
+ # Use the StrongParameters strategy in DecentExposure
31
+ decent_configuration do
32
+ strategy DecentExposure::StrongParametersStrategy
33
+ attributes :edit_params
34
+ except %w(index)
35
+ end
36
+
37
+ # Use the FlashResponder and HttpCacheResponder to respond to
38
+ # various requests.
39
+ responders :flash, :http_cache
40
+ end
41
+
42
+ module ClassMethods
43
+ # Initialize this controller as an authenticated resource.
44
+ def resource(name=nil, &block)
45
+ name = self.name.gsub(/Controller/, '').tableize if name.nil?
46
+ self._singleton_resource = name.to_s.singularize.to_sym
47
+ self._collection_resource = name.to_s.pluralize.to_sym
48
+
49
+ class_eval <<-RUBY
50
+ respond_to :html
51
+ expose :#{model}, except: %w(index)
52
+ expose :#{collection}, only: %w(index) do
53
+ #{model_class}.where(search_params)
54
+ end
55
+ #{authenticate if defined? Devise}
56
+ RUBY
57
+
58
+ yield if block_given?
59
+ end
60
+
61
+ protected
62
+ # Set the search params for this controller.
63
+ def search(*hash_of_params)
64
+ self._search_params = hash_of_params
65
+ end
66
+
67
+ # Set the edit params for this controller.
68
+ def modify(*hash_of_params)
69
+ self._edit_params = hash_of_params
70
+ end
71
+
72
+ private
73
+ def authenticate
74
+ "before_action :authenticate_user!, except: %w(index show)"
75
+ end
76
+
77
+ def model_class
78
+ @model_class ||= model.classify
79
+ end
80
+
81
+ def collection
82
+ @collection ||= self._collection_resource.to_s
83
+ end
84
+ end
85
+
86
+ def search_params
87
+ params.permit self.class._search_params
88
+ end
89
+
90
+ def edit_params
91
+ params.require(self.class._singleton_resource).permit self._edit_params
92
+ end
93
+ end
94
+ end
File without changes
@@ -0,0 +1,3 @@
1
+ module ControllerResources
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate install Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,16 @@
1
+ class InstallGenerator < Rails::Generators::NamedBase
2
+ source_root File.expand_path('../templates', __FILE__)
3
+
4
+ def copy_locale
5
+ copy_file "#{gem_path}/lib/responders/locales/en.yml", "responders.en.yml"
6
+ end
7
+
8
+ private
9
+ def gem_path
10
+ @gem_path ||= gem_spec.full_gem_path
11
+ end
12
+
13
+ def gem_spec
14
+ @gem_spec ||= Gem::Specification.find_by_name 'responders'
15
+ end
16
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :controller_resources do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ module ControllerResources
4
+ RSpec.describe Extension do
5
+ class MockController < ActionController::Base
6
+ include Extension
7
+
8
+ resource :model do
9
+ search :name
10
+ modify :name, :password
11
+ end
12
+ end
13
+
14
+ subject { MockController }
15
+ let(:controller) { subject.new }
16
+
17
+ it "defines singleton and collection resources" do
18
+ expect(subject._singleton_resource).to eq(:model)
19
+ expect(subject._collection_resource).to eq(:models)
20
+ end
21
+
22
+ it "saves params" do
23
+ expect(subject._search_params).to eq([:name])
24
+ expect(subject._edit_params).to eq([:name, :password])
25
+ end
26
+
27
+ it "publishes params to instance method" do
28
+ expect(controller).to respond_to(:search_params)
29
+ expect(controller).to respond_to(:edit_params)
30
+ end
31
+ end
32
+ 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
File without changes
@@ -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 vendor/assets/javascripts of plugins, if any, 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 .