resourcerer 1.0.0 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Resourcerer::Controller do
4
+ module Awesome
5
+ class Thing; end
6
+ end
7
+
8
+ Given(:controller_klass) { Class.new { include Resourcerer::Controller } }
9
+ Given(:params) { double('Params', :[] => nil) }
10
+ Given(:request) { double('Request', get?: false) }
11
+ Given(:controller) { controller_klass.new }
12
+ Given(:thing) { double('Awesome::Thing') }
13
+
14
+ def controller_thing
15
+ controller.send(:thing)
16
+ end
17
+
18
+ Given {
19
+ allow(controller).to receive(:action_name).and_return('edit')
20
+ allow(controller).to receive(:params).and_return(params)
21
+ allow(controller).to receive(:request).and_return(request)
22
+ }
23
+
24
+ delegate :resource, :resourcerer_config, to: :controller_klass
25
+
26
+ context 'permit' do
27
+ Given(:attrs) {{ name: 'Max', email: 'max@email.com' }}
28
+ Given(:fields) { [:name, :email] }
29
+
30
+ context 'allows to specify permitted attributes' do
31
+ Given {
32
+ require 'active_model'
33
+ expect(params).to receive(:require).with(:awesome_thing).and_return(params)
34
+ expect(params).to receive(:permit).with(*fields).and_return(attrs)
35
+ expect(Awesome::Thing).to receive(:new).with(attrs).and_return(thing)
36
+ }
37
+
38
+ after {
39
+ ActiveModel.send(:remove_const, 'Name')
40
+ }
41
+
42
+ context 'permit using a proc' do
43
+ When { resource :thing, model: Awesome::Thing, permit: fields }
44
+ Then { controller_thing == thing }
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,8 @@
1
+ require "simplecov"
2
+ require "coveralls"
3
+ SimpleCov.start { add_filter '/spec/' }
4
+ Coveralls.wear!
5
+
6
+ require "resourcerer"
7
+ require "rspec/given"
8
+ require "pry-byebug"
@@ -0,0 +1,60 @@
1
+ require "action_controller"
2
+ require "rails"
3
+
4
+ def request_params(params)
5
+ { params: params, **params }
6
+ end
7
+
8
+ module Rails
9
+ class App
10
+ def env_config; {} end
11
+
12
+ def routes
13
+ @routes ||= ActionDispatch::Routing::RouteSet.new.tap do |routes|
14
+ routes.draw do
15
+ resources :guitars
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ def self.root
22
+ ''
23
+ end
24
+
25
+ def self.application
26
+ @app ||= App.new
27
+ end
28
+ end
29
+
30
+ class Guitar
31
+ attr_accessor :name
32
+ def initialize(options = {})
33
+ options.each { |k, v| self.public_send("#{k}=", v) }
34
+ end
35
+ end
36
+
37
+ class ApplicationController < ActionController::Base
38
+ include Rails.application.routes.url_helpers
39
+ end
40
+
41
+ class GuitarsController < ApplicationController
42
+ resource :guitar
43
+ resource(:guitar?, model: Guitar, find: -> (id, model) { !!model.find(id) })
44
+ resource(:guitars) do
45
+ build { Guitar.all }
46
+ end
47
+
48
+ # To simplify testing.
49
+ public :guitars, :guitar, :guitar?
50
+
51
+ %i(index show edit new create update).each do |action|
52
+ define_method action do
53
+ head :ok
54
+ end
55
+ end
56
+
57
+ def guitar_params
58
+ params.require(:guitar).permit(:name)
59
+ end
60
+ end
metadata CHANGED
@@ -1,44 +1,114 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resourcerer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Máximo Mussini
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-08 00:00:00.000000000 Z
11
+ date: 2017-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '4.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activemodel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
25
39
  - !ruby/object:Gem::Version
26
40
  version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
- name: pakiderm
42
+ name: coveralls
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
- - - '>='
45
+ - - ">="
32
46
  - !ruby/object:Gem::Version
33
47
  version: '0'
34
- type: :runtime
48
+ type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
- - - '>='
52
+ - - ">="
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
41
- description: Define resources to automate finding a record and assigning attributes.
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: railties
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '4.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '4.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-given
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec-rails
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ description: Define resources to automatically find a record and assign its attributes.
42
112
  email:
43
113
  - maximomussini@gmail.com
44
114
  executables: []
@@ -48,42 +118,42 @@ extra_rdoc_files:
48
118
  files:
49
119
  - README.md
50
120
  - lib/resourcerer.rb
51
- - lib/resourcerer/configuration/strong_parameters.rb
52
- - lib/resourcerer/inflector.rb
121
+ - lib/resourcerer/configuration.rb
122
+ - lib/resourcerer/controller.rb
53
123
  - lib/resourcerer/resource.rb
54
- - lib/resourcerer/resource_configuration.rb
55
- - lib/resourcerer/resourceable.rb
56
- - lib/resourcerer/strategies/assign_attributes.rb
57
- - lib/resourcerer/strategies/assign_from_method.rb
58
- - lib/resourcerer/strategies/assign_from_params.rb
59
- - lib/resourcerer/strategies/default_strategy.rb
60
- - lib/resourcerer/strategies/eager_attributes_strategy.rb
61
- - lib/resourcerer/strategies/optional_strategy.rb
62
- - lib/resourcerer/strategies/strong_parameters_strategy.rb
63
- - lib/resourcerer/strategy.rb
124
+ - lib/resourcerer/version.rb
125
+ - spec/features/guitars_controller_spec.rb
126
+ - spec/resourcerer/controller_spec.rb
127
+ - spec/resourcerer/param_key_spec.rb
128
+ - spec/spec_helper.rb
129
+ - spec/support/rails_app.rb
64
130
  homepage: https://github.com/ElMassimo/resourcerer
65
131
  licenses:
66
132
  - MIT
67
133
  metadata: {}
68
134
  post_install_message:
69
- rdoc_options:
70
- - --charset=UTF-8
135
+ rdoc_options: []
71
136
  require_paths:
72
137
  - lib
73
138
  required_ruby_version: !ruby/object:Gem::Requirement
74
139
  requirements:
75
- - - '>='
140
+ - - "~>"
76
141
  - !ruby/object:Gem::Version
77
- version: '2.0'
142
+ version: '2.2'
78
143
  required_rubygems_version: !ruby/object:Gem::Requirement
79
144
  requirements:
80
- - - '>='
145
+ - - ">="
81
146
  - !ruby/object:Gem::Version
82
147
  version: '0'
83
148
  requirements: []
84
149
  rubyforge_project:
85
- rubygems_version: 2.2.2
150
+ rubygems_version: 2.5.1
86
151
  signing_key:
87
152
  specification_version: 4
88
153
  summary: Dry up your controllers by defining resources
89
- test_files: []
154
+ test_files:
155
+ - spec/features/guitars_controller_spec.rb
156
+ - spec/resourcerer/controller_spec.rb
157
+ - spec/resourcerer/param_key_spec.rb
158
+ - spec/spec_helper.rb
159
+ - spec/support/rails_app.rb
@@ -1,12 +0,0 @@
1
- require 'resourcerer/resource_configuration'
2
-
3
- module Resourcerer
4
- module Configuration
5
- class StrongParameters < ResourceConfiguration
6
-
7
- def permit(attrs)
8
- options[:permitted_attributes] = attrs
9
- end
10
- end
11
- end
12
- end
@@ -1,33 +0,0 @@
1
- require 'active_support/inflector'
2
- require 'active_support/core_ext/string'
3
- require 'active_model/naming'
4
-
5
- module Resourcerer
6
- class Inflector
7
- attr_reader :original, :model, :finder_attribute
8
-
9
- def initialize(name, model, finder_attribute=:id)
10
- @original, @model, @finder_attribute = name.to_s, model, finder_attribute
11
- end
12
-
13
- alias name original
14
-
15
- def model_name
16
- @model_name ||= model.model_name
17
- rescue
18
- @model_name = ActiveModel::Name.new(model)
19
- end
20
-
21
- def param_key
22
- model_name.param_key
23
- end
24
-
25
- def finder_param
26
- "#{model_name.singular}_#{finder_attribute}"
27
- end
28
-
29
- def self.class_for(name)
30
- name.to_s.split('::').map(&:classify).join('::').constantize
31
- end
32
- end
33
- end
@@ -1,49 +0,0 @@
1
- # This is the base class that is intended to build the DSL for Resourcer
2
- module Resourcerer
3
- class ResourceConfiguration
4
-
5
- attr_reader :options
6
-
7
- def initialize(options={})
8
- @options = options || {}
9
- end
10
-
11
- def method_missing(name, *args)
12
- super if args.size > 1
13
- define_option_method(name)
14
- send(name, args.first)
15
- end
16
-
17
- def define_option_method(name)
18
- # We chose not caching the value inside the method because it's more flexible
19
- self.class.class_eval <<-EVAL
20
- def #{name}(value=nil)
21
- options[:#{name}] = value if value
22
- options[:#{name}]
23
- end
24
- EVAL
25
- end
26
-
27
- # DSL
28
-
29
- def find(&block)
30
- options[:finder] = block
31
- end
32
-
33
- def build(&block)
34
- options[:builder] = block
35
- end
36
-
37
- def assign(&block)
38
- options[:attributes] = block
39
- end
40
-
41
- def collection(proc=nil, &block)
42
- if proc = (proc || block)
43
- options[:collection] = proc
44
- else
45
- options[:collection]
46
- end
47
- end
48
- end
49
- end
@@ -1,44 +0,0 @@
1
- require 'resourcerer/resource'
2
- require 'resourcerer/strategies/optional_strategy'
3
- require 'pakiderm'
4
-
5
- module Resourcerer
6
- module Resourceable
7
- extend ActiveSupport::Concern
8
-
9
- included do
10
- extend Pakiderm
11
- end
12
-
13
- def resource(name, options={}, &block)
14
- Resource.for(name, options.merge(strategy: Strategies::OptionalStrategy), block).call(self)
15
- end
16
-
17
- module ClassMethods
18
-
19
- def _resources
20
- @_resources ||= {}
21
- end
22
-
23
- def resource(name, options={}, &block)
24
- check_method_available(name)
25
- _resources[name] = resource = Resource.for(name, options, block)
26
- define_resource_method(name, resource)
27
- end
28
-
29
- private
30
-
31
- def define_resource_method(name, resource)
32
- define_method(name) { resource.call(self) }
33
- memoize name, assignable: true
34
- hide_action name, "#{name}="
35
- end
36
-
37
- def check_method_available(name)
38
- if self.respond_to?(name.to_sym)
39
- Kernel.warn "[Resourcerer] Overriding #{name} method."
40
- end
41
- end
42
- end
43
- end
44
- end