controller_resources 0.1.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NTQ3MGFiNmFiMDExMjIxMGJlYWI5NDc3ZDQ0OTVhM2I1YzY5Y2U4Mg==
4
+ NDliMjAxNWIzOGY5NjE2NzZmNTg3ZDRjMGEwZDdmOTA3NWZjOTM3Mw==
5
5
  data.tar.gz: !binary |-
6
- NzQ3NGYwYTQ5N2IyOGM3MzJiOWZiNDVkZDE5ODRlNWQ1ODE4MjYyZA==
6
+ YzY5YWZkY2YwMDQxNjc2NjA4MjlmMjNkMmRmZDBhNzI0NzZmZGM2YQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MmFmN2NjM2MwMThhYmViMWM0YzMxNDc0ZmU1NTM5ZTQ3MWIwNWFkY2M3NmQ5
10
- YmI4NDM3ZWViNTFjOWEyY2JiMWM4OWQzM2I4YTBhZDEyNWFjZjA2OGExMzEx
11
- YTdhNWU5Y2Q4NGJhZDczMmMxYWU4OWEyZDJkYzdhY2RkMjg3ODU=
9
+ ZTYxMmUxYjZiODFmMmRkNTRlNmM0YTc3ZGNmNmQ3MjhjNjA0NjE1NzYzNTI1
10
+ MWU2MWY1MDE0ZGFhNzkyMGY0YWQ2NDZkYjg4MDYwNDExMTZjODQxMjZkYTlh
11
+ Zjc1ZDFiZjRhOGY0NTMzZTAyNjY2MTRhYmMxNjVhMTJkMzBlMTE=
12
12
  data.tar.gz: !binary |-
13
- YThiOTdkZTZhODUyYzc2ZGY1MTljYmRkMDMwY2YwODcyNThhY2I4NTc1MTQz
14
- YjEyYWY2ZDA1ZWZmMmJkZTRkN2U5ODY1NTgwOGFiOTQ0ZmI5ZTRjNThhMmUy
15
- ZDExOTYyNzY5ZWE5YjdmY2FjNmFlYjJlZGY0MGM3Y2VmYTM2NTk=
13
+ OWJmYzY1MDQ5ODgwY2RlYTBiNWQ2N2Y3YTk0YTFjOTRhNjI3YjhkMTM4OWQ3
14
+ MjdkM2Q5NjA3OWRjODZlNTUxYTZjYTRlMDQ1ZDIwNGVjZmY2ZDkyYzJmMDkx
15
+ YmUwMmIzYmY4M2UxMTRiYzNmNjRkOTBhOGRjYjUyNGI1NjRkMDg=
@@ -1 +1 @@
1
- 2.2.2
1
+ 2.3.1
@@ -1,10 +1,8 @@
1
1
  language: ruby
2
2
  sudo: false
3
- cache: bundle
3
+ cache: bundler
4
4
  rvm:
5
- - 2.0.0
6
- - 2.1.6
7
- - 2.2.2
5
+ - 2.3.1
8
6
  addons:
9
7
  codeclimate:
10
8
  repo_token:
@@ -17,4 +15,3 @@ deploy:
17
15
  on:
18
16
  repo: tubbo/controller_resources
19
17
  tags: true
20
- rvm: 2.2.2
data/README.md CHANGED
@@ -6,35 +6,26 @@
6
6
  [![Inline docs](http://inch-ci.org/github/tubbo/controller_resources.svg?branch=master)](http://inch-ci.org/github/tubbo/controller_resources)
7
7
 
8
8
  A Rails engine providing a common DSL for fetching model resources in
9
- the controller and view layers. It leverages
10
- [DecentExposure][de], [StrongParameters][sp] and assumes an
11
- ActiveRecord-like DSL for querying model objects.
12
- ControllerResources does not assume any part of your stack, instead
13
- providing generic tools and extensions to ActionController which allow
14
- you to use the fetched resources however you want.
9
+ the controller and view layers.
15
10
 
16
11
  ```ruby
17
- resource :post do
18
- permit :title, :category, :body, :is_published
12
+ class PostsController < ApplicationController
13
+ resource :post
14
+ respond_to :html
15
+
16
+ def index
17
+ respond_with @posts
18
+ end
19
19
  end
20
20
  ```
21
21
 
22
- ...and not have to worry about where the `posts` and `post` methods come
23
- from. If you're used to working with DecentExposure, you'll know that
24
- we're just using the `expose` macro to set up these resources, and using
25
- the `resource` macro to populate what we expose and additionally what
26
- parameters to pass through.
22
+ You can also specify an ancestor, which is used to look up the given
23
+ resource.
27
24
 
28
- You can establish DecentExposure configuration with the `resource` block
29
- by calling methods which do not exist on the Resource. All of these
30
- methods are passed down to DecentExposure:
31
25
 
32
- ```ruby
33
- expose :post, param: :post_id
34
- resource :comment, ancestor: :post do
35
- permit :body, :user_id
36
- end
37
- ```
26
+ In this example, `@comment` is being looked up using
27
+ `@post.comments.find` rather than the default `Comment.find`.
28
+
38
29
 
39
30
  ## Installation
40
31
 
@@ -50,38 +41,54 @@ And run
50
41
  $ bundle install
51
42
  ```
52
43
 
44
+ Then, include the module in your base controller class:
45
+
46
+ ```ruby
47
+ class ApplicationController < ActionController::Base
48
+ include ControllerResources
49
+ end
50
+ ```
51
+
53
52
  ## Usage
54
53
 
55
- Define your resource in the controller, and you can use methods instead
56
- of instance variables to access the model object. No more writing finder
57
- methods!
54
+ Using the `resource` macro, you can define the name of your resource
55
+ which will be used to derive instance variable names and class names for
56
+ your models. ControllerResources assumes that your instance variables
57
+ are named conventionally, and assumes that you follow Rails best
58
+ practices when assigning instance variables in the controller.
58
59
 
59
60
  ```ruby
60
- class ItemsController < ApplicationController
61
- resource :item do
62
- permit :name, :user, :is_private
63
- end
61
+ class CommentsController < ApplicationController
62
+ before_action :find_post
63
+ resource :comment, ancestor: :post
64
+ respond_to :html
64
65
 
65
- def index
66
- respond_with items
66
+ def show
67
+ respond_with @comment
67
68
  end
68
69
 
69
- def show
70
- respond_with item
70
+ def new
71
+ @comment = @post.comments.build
71
72
  end
72
73
 
73
74
  def create
74
- item.save
75
- respond_with item
75
+ authorize @comment
76
+ @comment = @post.comments.create permitted_params(Comment)
77
+ respond_with @comment
78
+ end
79
+
80
+ def update
81
+ authorize @comment
82
+ @comment.update permitted_params(Comment)
83
+ respond_with @comment
76
84
  end
77
- end
78
- ```
79
85
 
80
- In your view, you can use methods instead of instance variables to
81
- access the model objects passed down into the template:
86
+ private
82
87
 
83
- ```erb
84
- <%= user.name %>
88
+ def find_post
89
+ @post = Post.find params[:post_id]
90
+ end
91
+ end
85
92
  ```
86
93
 
87
94
  ### Meta-Programming Capabilities
@@ -95,6 +102,34 @@ your view as well as the controller.
95
102
 
96
103
  For more, consult the [RDoc Documentation][rdoc]
97
104
 
105
+ ### Customization
106
+
107
+ As said before, the `model` and `collection` methods are exposed for you
108
+ in the controller, and are what is used as the values for the instance
109
+ variables set in `:find_resource`. You can override these methods in
110
+ your base controller class to perform authorization or decoration logic
111
+ in a consistent manner, like so:
112
+
113
+ ```ruby
114
+ class ApplicationController < ActionController::Base
115
+ include ControllerResources
116
+
117
+ private
118
+
119
+ def model
120
+ super.tap do |record|
121
+ authorize record
122
+ end.decorate
123
+ end
124
+
125
+ def collection
126
+ super.tap do |records|
127
+ policy_scope records
128
+ end.decorate
129
+ end
130
+ end
131
+ ```
132
+
98
133
  ## Contributing
99
134
 
100
135
  Contributions to `ControllerResources` may be made using GitHub pull
data/Rakefile CHANGED
@@ -6,10 +6,9 @@ require 'controller_resources/version'
6
6
  require 'yard'
7
7
  require 'travis/release/task'
8
8
 
9
- desc 'Set up the test database for the dummy app'
10
- task :db do
11
- sh 'cd spec/dummy && bundle exec rake db:setup'
12
- end
9
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
10
+ load 'rails/tasks/engine.rake'
11
+ load 'rails/tasks/statistics.rake'
13
12
 
14
13
  # Run RSpec code examples
15
14
  RSpec::Core::RakeTask.new :test
@@ -27,7 +26,7 @@ YARD::Rake::YardocTask.new :doc
27
26
  Travis::Release::Task.new
28
27
 
29
28
  # CI task
30
- task default: %i(lint db test build)
29
+ task default: %i(app:db:setup test build)
31
30
 
32
31
  namespace :ci do
33
32
  desc "Rename gem package so it can be released with GitHub."
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
  #
3
4
  # This file was generated by Bundler.
4
5
  #
@@ -6,11 +7,11 @@
6
7
  # this file is here to facilitate running it.
7
8
  #
8
9
 
9
- require 'pathname'
10
- ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
11
12
  Pathname.new(__FILE__).realpath)
12
13
 
13
- require 'rubygems'
14
- require 'bundler/setup'
14
+ require "rubygems"
15
+ require "bundler/setup"
15
16
 
16
- load Gem.bin_path('codeclimate-test-reporter', 'cc-tddium-post-worker')
17
+ load Gem.bin_path("codeclimate-test-reporter", "cc-tddium-post-worker")
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
  #
3
4
  # This file was generated by Bundler.
4
5
  #
@@ -6,11 +7,11 @@
6
7
  # this file is here to facilitate running it.
7
8
  #
8
9
 
9
- require 'pathname'
10
- ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
11
12
  Pathname.new(__FILE__).realpath)
12
13
 
13
- require 'rubygems'
14
- require 'bundler/setup'
14
+ require "rubygems"
15
+ require "bundler/setup"
15
16
 
16
- load Gem.bin_path('coderay', 'coderay')
17
+ load Gem.bin_path("coderay", "coderay")
data/bin/erubis CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
  #
3
4
  # This file was generated by Bundler.
4
5
  #
@@ -6,11 +7,11 @@
6
7
  # this file is here to facilitate running it.
7
8
  #
8
9
 
9
- require 'pathname'
10
- ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
11
12
  Pathname.new(__FILE__).realpath)
12
13
 
13
- require 'rubygems'
14
- require 'bundler/setup'
14
+ require "rubygems"
15
+ require "bundler/setup"
15
16
 
16
- load Gem.bin_path('erubis', 'erubis')
17
+ load Gem.bin_path("erubis", "erubis")
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
  #
3
4
  # This file was generated by Bundler.
4
5
  #
@@ -6,11 +7,11 @@
6
7
  # this file is here to facilitate running it.
7
8
  #
8
9
 
9
- require 'pathname'
10
- ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
11
12
  Pathname.new(__FILE__).realpath)
12
13
 
13
- require 'rubygems'
14
- require 'bundler/setup'
14
+ require "rubygems"
15
+ require "bundler/setup"
15
16
 
16
- load Gem.bin_path('diff-lcs', 'htmldiff')
17
+ load Gem.bin_path("diff-lcs", "htmldiff")
data/bin/ldiff CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
  #
3
4
  # This file was generated by Bundler.
4
5
  #
@@ -6,11 +7,11 @@
6
7
  # this file is here to facilitate running it.
7
8
  #
8
9
 
9
- require 'pathname'
10
- ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
11
12
  Pathname.new(__FILE__).realpath)
12
13
 
13
- require 'rubygems'
14
- require 'bundler/setup'
14
+ require "rubygems"
15
+ require "bundler/setup"
15
16
 
16
- load Gem.bin_path('diff-lcs', 'ldiff')
17
+ load Gem.bin_path("diff-lcs", "ldiff")
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
  #
3
4
  # This file was generated by Bundler.
4
5
  #
@@ -6,11 +7,11 @@
6
7
  # this file is here to facilitate running it.
7
8
  #
8
9
 
9
- require 'pathname'
10
- ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
11
12
  Pathname.new(__FILE__).realpath)
12
13
 
13
- require 'rubygems'
14
- require 'bundler/setup'
14
+ require "rubygems"
15
+ require "bundler/setup"
15
16
 
16
- load Gem.bin_path('nokogiri', 'nokogiri')
17
+ load Gem.bin_path("nokogiri", "nokogiri")
data/bin/pry CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
  #
3
4
  # This file was generated by Bundler.
4
5
  #
@@ -6,11 +7,11 @@
6
7
  # this file is here to facilitate running it.
7
8
  #
8
9
 
9
- require 'pathname'
10
- ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
11
12
  Pathname.new(__FILE__).realpath)
12
13
 
13
- require 'rubygems'
14
- require 'bundler/setup'
14
+ require "rubygems"
15
+ require "bundler/setup"
15
16
 
16
- load Gem.bin_path('pry', 'pry')
17
+ load Gem.bin_path("pry", "pry")
data/bin/rackup CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
  #
3
4
  # This file was generated by Bundler.
4
5
  #
@@ -6,11 +7,11 @@
6
7
  # this file is here to facilitate running it.
7
8
  #
8
9
 
9
- require 'pathname'
10
- ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
11
12
  Pathname.new(__FILE__).realpath)
12
13
 
13
- require 'rubygems'
14
- require 'bundler/setup'
14
+ require "rubygems"
15
+ require "bundler/setup"
15
16
 
16
- load Gem.bin_path('rack', 'rackup')
17
+ load Gem.bin_path("rack", "rackup")
data/bin/rails CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
  #
3
4
  # This file was generated by Bundler.
4
5
  #
@@ -6,11 +7,11 @@
6
7
  # this file is here to facilitate running it.
7
8
  #
8
9
 
9
- require 'pathname'
10
- ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
11
12
  Pathname.new(__FILE__).realpath)
12
13
 
13
- require 'rubygems'
14
- require 'bundler/setup'
14
+ require "rubygems"
15
+ require "bundler/setup"
15
16
 
16
- load Gem.bin_path('railties', 'rails')
17
+ load Gem.bin_path("railties", "rails")
data/bin/rake CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
  #
3
4
  # This file was generated by Bundler.
4
5
  #
@@ -6,11 +7,11 @@
6
7
  # this file is here to facilitate running it.
7
8
  #
8
9
 
9
- require 'pathname'
10
- ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
11
12
  Pathname.new(__FILE__).realpath)
12
13
 
13
- require 'rubygems'
14
- require 'bundler/setup'
14
+ require "rubygems"
15
+ require "bundler/setup"
15
16
 
16
- load Gem.bin_path('rake', 'rake')
17
+ load Gem.bin_path("rake", "rake")