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.
@@ -2,13 +2,13 @@
2
2
 
3
3
  <p>
4
4
  <strong>Title:</strong>
5
- <%= post.title %>
5
+ <%= @post.title %>
6
6
  </p>
7
7
 
8
8
  <p>
9
9
  <strong>Body:</strong>
10
- <%= post.body %>
10
+ <%= @post.body %>
11
11
  </p>
12
12
 
13
- <%= link_to 'Edit', edit_post_path(post) %> |
13
+ <%= link_to 'Edit', edit_post_path(@post) %> |
14
14
  <%= link_to 'Back', posts_path %>
@@ -22,5 +22,4 @@ ActiveRecord::Schema.define(version: 20150614211054) do
22
22
  t.datetime "created_at", null: false
23
23
  t.datetime "updated_at", null: false
24
24
  end
25
-
26
25
  end
@@ -1,2 +1,2 @@
1
- puts 'creating post record'
2
- Post.create title: 'title', body: 'body'
1
+ # puts 'creating post record'
2
+ # Post.create title: 'title', body: 'body'
@@ -1,6 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  RSpec.feature 'posts' do
4
+ fixtures :posts
5
+
4
6
  let :post do
5
7
  posts :post
6
8
  end
@@ -20,29 +22,34 @@ RSpec.feature 'posts' do
20
22
  end
21
23
 
22
24
  scenario 'creating a new post' do
23
- skip
24
25
  visit new_post_path
25
26
 
26
27
  fill_in 'Title', with: 'new post'
27
28
  fill_in 'Body', with: 'this is a new post'
28
29
  click_button 'Save'
29
30
 
30
- expect(page).to have_content 'Listing Posts'
31
+ expect(page).to have_content 'Post created.'
32
+ expect(page).to have_content 'Title: new post'
31
33
  end
32
34
 
33
35
  scenario 'editing an existing post' do
34
- skip
35
36
  visit edit_post_path(post)
36
37
 
37
38
  fill_in 'Title', with: 'new title'
38
39
  click_button 'Save'
39
40
 
40
- expect(page).to have_content 'new title'
41
+ expect(page).to have_content 'Post updated.'
42
+ expect(page).to have_content 'Title: new title'
41
43
  end
42
44
 
43
45
  scenario 'deleting an existing post' do
44
- visit posts_path(post, method: :delete)
46
+ visit posts_path
47
+
48
+ within "#post-#{post.id}" do
49
+ click_link 'Destroy'
50
+ end
45
51
 
46
- expect(page).to have_content 'Listing Posts'
52
+ expect(page).to have_content 'Post deleted'
53
+ expect(page).not_to have_content post.title
47
54
  end
48
55
  end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ class TestController
4
+ include ControllerResources
5
+
6
+ def self.before_action(*args)
7
+ true
8
+ end
9
+
10
+ resource :comment, ancestor: :post
11
+ end
12
+
13
+
14
+ RSpec.describe ControllerResources do
15
+ let :controller do
16
+ TestController
17
+ end
18
+
19
+ it 'can be included into a controller' do
20
+ expect(controller.ancestors).to include(ControllerResources)
21
+ end
22
+
23
+ it 'defines resource name' do
24
+ expect(controller.resource_name).to eq 'comment'
25
+ end
26
+
27
+ it 'defines ancestor name' do
28
+ expect(controller.resource_ancestor_name).to eq 'post'
29
+ end
30
+
31
+ it 'has default collection actions but can add over time' do
32
+ expect(controller.collection_actions).to eq([:index])
33
+ controller.collection_actions << :show
34
+ expect(controller.collection_actions).to eq([:index, :show])
35
+ end
36
+ end
@@ -7,12 +7,8 @@ ENV['RAILS_ENV'] ||= 'test'
7
7
  require File.expand_path("../dummy/config/environment.rb", __FILE__)
8
8
  require 'rspec/rails'
9
9
  require 'capybara/rspec'
10
- # require 'capybara/poltergeist'
11
- require 'database_cleaner'
12
10
  require 'pry'
13
-
14
- # Perform all DB operations within a transaction.
15
- DatabaseCleaner.strategy = :transaction
11
+ # require 'rails/test_help'
16
12
 
17
13
  # Clean backtraces
18
14
  Rails.backtrace_cleaner.remove_silencers!
@@ -20,8 +16,6 @@ Rails.backtrace_cleaner.remove_silencers!
20
16
  # Load support files
21
17
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
22
18
 
23
- # Capybara.javascript_driver = :poltergeist
24
-
25
19
  # Configure RSpec with `--init`-based options.
26
20
  RSpec.configure do |config|
27
21
  config.fixture_path = File.expand_path('../fixtures', __FILE__)
@@ -101,12 +95,4 @@ RSpec.configure do |config|
101
95
  # test failures related to randomization by passing the same `--seed` value
102
96
  # as the one that triggered the failure.
103
97
  Kernel.srand config.seed
104
-
105
- config.before do
106
- DatabaseCleaner.start
107
- end
108
-
109
- config.after do
110
- DatabaseCleaner.clean
111
- end
112
98
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: controller_resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Scott
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-26 00:00:00.000000000 Z
11
+ date: 2016-07-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -182,30 +182,16 @@ dependencies:
182
182
  name: rails
183
183
  requirement: !ruby/object:Gem::Requirement
184
184
  requirements:
185
- - - ~>
185
+ - - ! '>='
186
186
  - !ruby/object:Gem::Version
187
- version: '4'
188
- type: :runtime
189
- prerelease: false
190
- version_requirements: !ruby/object:Gem::Requirement
191
- requirements:
192
- - - ~>
193
- - !ruby/object:Gem::Version
194
- version: '4'
195
- - !ruby/object:Gem::Dependency
196
- name: decent_exposure
197
- requirement: !ruby/object:Gem::Requirement
198
- requirements:
199
- - - ~>
200
- - !ruby/object:Gem::Version
201
- version: '2'
187
+ version: '0'
202
188
  type: :runtime
203
189
  prerelease: false
204
190
  version_requirements: !ruby/object:Gem::Requirement
205
191
  requirements:
206
- - - ~>
192
+ - - ! '>='
207
193
  - !ruby/object:Gem::Version
208
- version: '2'
194
+ version: '0'
209
195
  description: ! "A controller DSL for Rails that allows you to easily and quickly define
210
196
  both singular and collection model resources that can be operated on within the
211
197
  controller.Attempts to DRY up most of the boilerplate code at the top of\n each
@@ -247,9 +233,6 @@ files:
247
233
  - bin/yri
248
234
  - controller_resources.gemspec
249
235
  - lib/controller_resources.rb
250
- - lib/controller_resources/engine.rb
251
- - lib/controller_resources/extension.rb
252
- - lib/controller_resources/not_defined_error.rb
253
236
  - lib/controller_resources/version.rb
254
237
  - spec/dummy/README.rdoc
255
238
  - spec/dummy/Rakefile
@@ -307,7 +290,7 @@ files:
307
290
  - spec/features/posts_spec.rb
308
291
  - spec/fixtures/posts.yml
309
292
  - spec/integration/navigation_spec.rb
310
- - spec/lib/controller_resources/extension_spec.rb
293
+ - spec/lib/controller_resources_spec.rb
311
294
  - spec/spec_helper.rb
312
295
  - spec/support/posts.rb
313
296
  homepage: https://github.com/tubbo/controller_resources
@@ -367,9 +350,6 @@ test_files:
367
350
  - bin/yri
368
351
  - controller_resources.gemspec
369
352
  - lib/controller_resources.rb
370
- - lib/controller_resources/engine.rb
371
- - lib/controller_resources/extension.rb
372
- - lib/controller_resources/not_defined_error.rb
373
353
  - lib/controller_resources/version.rb
374
354
  - spec/dummy/README.rdoc
375
355
  - spec/dummy/Rakefile
@@ -427,6 +407,6 @@ test_files:
427
407
  - spec/features/posts_spec.rb
428
408
  - spec/fixtures/posts.yml
429
409
  - spec/integration/navigation_spec.rb
430
- - spec/lib/controller_resources/extension_spec.rb
410
+ - spec/lib/controller_resources_spec.rb
431
411
  - spec/spec_helper.rb
432
412
  - spec/support/posts.rb
@@ -1,9 +0,0 @@
1
- module ControllerResources
2
- # Configures the +ControllerResources+ to be isolated and
3
- # eager-loaded.
4
- class Engine < ::Rails::Engine
5
- isolate_namespace ControllerResources
6
-
7
- config.eager_load_namespaces << ControllerResources
8
- end
9
- end
@@ -1,147 +0,0 @@
1
- module ControllerResources
2
- # The DSL mixin for ActionController that allows you to quickly define
3
- # both singular and collection model resources that can be operated on
4
- # within the controller. Attempts to DRY up most of the boilerplate code
5
- # at the top of each controller used to set up its state.
6
- #
7
- # @author Tom Scott
8
- #
9
- # @example
10
- #
11
- # class PostsController < ApplicationController
12
- # resource :post do
13
- # permit :title, :body, :author_id
14
- # end
15
- #
16
- # def index
17
- # respond_with posts
18
- # end
19
- #
20
- # def show
21
- # respond_with post
22
- # end
23
- #
24
- # def create
25
- # post.save
26
- # respond_with post
27
- # end
28
- #
29
- module Extension
30
- extend ActiveSupport::Concern
31
-
32
- included do
33
- # Given name of the model as supplied to the +resource+ directive.
34
- #
35
- # @attr_accessor [Symbol]
36
- class_attribute :model_name
37
-
38
- # Collection name computed by pluralizing the given model name.
39
- #
40
- # @attr_accessor [Symbol]
41
- class_attribute :collection_name
42
-
43
- # A collection of arguments supplied to +StrongParameters#permit+
44
- # that is used in the +edit_params+ method to easily define
45
- # attributes which need to be whitelisted for mass assignment.
46
- #
47
- # @attr_accessor [Array]
48
- class_attribute :params_to_permit
49
-
50
- # Configures +DecentExposure+ to use the +edit_params+ method
51
- # defined by this module as the default attributes for mass
52
- # assignment.
53
- decent_configuration do
54
- attributes :edit_params
55
- end
56
-
57
- helper_method :model, :collection
58
- end
59
-
60
- class_methods do
61
- # Define resources based on the given name and decent exposure
62
- # options. Uses +DecentExposure+ to define said resources and
63
- # stores the generated names in the controller's configuration.
64
- #
65
- # If a block is given, one can also call additional DSL methods
66
- # like +permit+.
67
- #
68
- # @param [Symbol] name - the name of the model exposure, which
69
- # is pluralized to compute the name of the collection exposure and
70
- # stored to +model_name+.
71
- # @param [Hash] options - passed wholesale to both +expose+ methods,
72
- # and useful for defining custom +DecentExposure+ options.
73
- # @param &block - a block that is yielded after +expose+ methods are
74
- # defined, and used primarily to set permittable params.
75
- # @example
76
- #
77
- # expose :author
78
- # resource :post, ancestor: :author
79
- #
80
- def resource(name, options = {})
81
- self.model_name = name
82
- self.collection_name = "#{name}".pluralize.to_sym
83
-
84
- expose model_name, options
85
- expose collection_name, options
86
-
87
- yield if block_given?
88
- end
89
-
90
- private
91
-
92
- # Permit the given attributes in +StrongParameters+.
93
- #
94
- # @example
95
- #
96
- # expose :author
97
- # resource :post, ancestor: :author do
98
- # permit :title, :body
99
- # end
100
- #
101
- def permit(*permittable_params)
102
- self.params_to_permit = permittable_params
103
- end
104
- end
105
-
106
- # Reader method for the defined singular resource.
107
- #
108
- # @return [Object] the model object or +nil+ if no resource has been
109
- # defined.
110
- def model
111
- return unless resource?
112
- public_send model_name
113
- end
114
-
115
- # Reader method for the defined collection resource.
116
- #
117
- # @return [Object] the collection of models or +nil+ if no resource
118
- # has been defined.
119
- def collection
120
- return unless resource?
121
- public_send collection_name
122
- end
123
-
124
- # White-listed parameters for mass assignment as defined by
125
- # +StrongParameters+. Throws a +ControllerResources::NotDefinedError+
126
- # if no +resource+ block has been defined on this controller.
127
- #
128
- # @return [ActionController::Parameters] the +StrongParameters+ hash.
129
- # @raise [ControllerResources::NotDefinedError] when no +resource+ has
130
- # been defined on this controller.
131
- def edit_params
132
- fail NotDefinedError unless resource?
133
- return params.require(model_name).permit! unless params_to_permit.present?
134
- params.require(model_name).permit(*params_to_permit)
135
- end
136
-
137
- private
138
-
139
- # Whether a +resource+ directive has been invoked on this controller.
140
- #
141
- # @private
142
- # @return [Boolean]
143
- def resource?
144
- model_name.present?
145
- end
146
- end
147
- end
@@ -1,12 +0,0 @@
1
- module ControllerResources
2
- # Thrown when a +resource+ has not been defined, yet the controller
3
- # attempts to call the generated +edit_params+ method.
4
- class NotDefinedError < RuntimeError
5
- def initialize(*args)
6
- super
7
- @message = %(
8
- Call to #edit_params failed: Resource not defined.
9
- )
10
- end
11
- end
12
- end
@@ -1,46 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module ControllerResources
4
- RSpec.describe Extension, type: :lib do
5
- subject do
6
- TestController.new
7
- end
8
-
9
- before do
10
- subject.class_eval do
11
- include Extension
12
-
13
- resource :post do
14
- permit :title
15
- end
16
- end
17
-
18
- allow(subject).to receive(:params).and_return(
19
- double('Parameters', require: double('Required', permit: true))
20
- )
21
- end
22
-
23
- it 'defines model_name' do
24
- expect(subject.model_name).to eq :post
25
- end
26
-
27
- it 'defines collection_name' do
28
- expect(subject.collection_name).to eq :posts
29
- end
30
-
31
- it 'computes model from model_name' do
32
- expect(subject).to respond_to :model
33
- expect(subject.post).to eq subject.model
34
- end
35
-
36
- it 'computes collection from collection_name' do
37
- expect(subject).to respond_to :collection
38
- expect(subject.collection).to include(subject.model)
39
- expect(subject.posts).to eq subject.collection
40
- end
41
-
42
- it 'defines edit_params' do
43
- expect(subject.edit_params).to be_present
44
- end
45
- end
46
- end