strong_resources 0.1.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c3a132203f868d2de62a7a7f2a94995abc8f7191
4
+ data.tar.gz: 40ed917362aa5bf2c261151cfc2f2527f916eb22
5
+ SHA512:
6
+ metadata.gz: db8f64a86a7e6e7f2ea426f2c5c9e6d891696bab3b8cd3fa64bb65dd6b8f41e59ee92d5ab5df57b0ee08ccb700c40803f352d8068b818d0cf8a80aaf8b329139
7
+ data.tar.gz: 097012051ac19679cbb017296c52b2aef2fe7fee22298832ebb6c8d2330857f7261d5e7489a55949e12ba3babf771dd2b80da413486c26d0a708a5683d10a696
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ bin
11
+ *.swp
12
+ *.swo
13
+ *.swn
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.11.2
data/Appraisals ADDED
@@ -0,0 +1,7 @@
1
+ appraise "rails-4" do
2
+ gem "rails", "~> 4.1"
3
+ end
4
+
5
+ appraise "rails-5" do
6
+ gem "rails", "~> 5.0"
7
+ end
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in strong_resources.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'appraisal'
8
+ gem 'pry'
9
+ gem 'pry-byebug'
10
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Lee Richmond
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # StrongResources
2
+
3
+ View [official documentation](https://jsonapi-suite.github.io/strong_resources)
4
+
5
+ Supported Rails versions: >= 4.1
6
+
7
+ ### Running the tests
8
+
9
+ This library tests multiple rails versions via [appraisal](https://github.com/thoughtbot/appraisal). Run using:
10
+
11
+ ```
12
+ bin/appraisal rails-4 rspec
13
+ bin/appraisal rails-5 rspec
14
+ ```
15
+
16
+ Or `bin/appraisal rspec` to run everything.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,13 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 4.1"
6
+
7
+ group :test do
8
+ gem "appraisal"
9
+ gem "pry"
10
+ gem "pry-byebug"
11
+ end
12
+
13
+ gemspec :path => "../"
@@ -0,0 +1,161 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ strong_resources (0.1.0)
5
+ rails (>= 4.1, < 6.0)
6
+ stronger_parameters (~> 2.6)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actionmailer (4.2.6)
12
+ actionpack (= 4.2.6)
13
+ actionview (= 4.2.6)
14
+ activejob (= 4.2.6)
15
+ mail (~> 2.5, >= 2.5.4)
16
+ rails-dom-testing (~> 1.0, >= 1.0.5)
17
+ actionpack (4.2.6)
18
+ actionview (= 4.2.6)
19
+ activesupport (= 4.2.6)
20
+ rack (~> 1.6)
21
+ rack-test (~> 0.6.2)
22
+ rails-dom-testing (~> 1.0, >= 1.0.5)
23
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
24
+ actionview (4.2.6)
25
+ activesupport (= 4.2.6)
26
+ builder (~> 3.1)
27
+ erubis (~> 2.7.0)
28
+ rails-dom-testing (~> 1.0, >= 1.0.5)
29
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
30
+ activejob (4.2.6)
31
+ activesupport (= 4.2.6)
32
+ globalid (>= 0.3.0)
33
+ activemodel (4.2.6)
34
+ activesupport (= 4.2.6)
35
+ builder (~> 3.1)
36
+ activerecord (4.2.6)
37
+ activemodel (= 4.2.6)
38
+ activesupport (= 4.2.6)
39
+ arel (~> 6.0)
40
+ activesupport (4.2.6)
41
+ i18n (~> 0.7)
42
+ json (~> 1.7, >= 1.7.7)
43
+ minitest (~> 5.1)
44
+ thread_safe (~> 0.3, >= 0.3.4)
45
+ tzinfo (~> 1.1)
46
+ appraisal (2.1.0)
47
+ bundler
48
+ rake
49
+ thor (>= 0.14.0)
50
+ arel (6.0.3)
51
+ builder (3.2.2)
52
+ byebug (9.0.5)
53
+ coderay (1.1.1)
54
+ concurrent-ruby (1.0.2)
55
+ diff-lcs (1.2.5)
56
+ erubis (2.7.0)
57
+ globalid (0.3.7)
58
+ activesupport (>= 4.1.0)
59
+ i18n (0.7.0)
60
+ json (1.8.3)
61
+ loofah (2.0.3)
62
+ nokogiri (>= 1.5.9)
63
+ mail (2.6.4)
64
+ mime-types (>= 1.16, < 4)
65
+ method_source (0.8.2)
66
+ mime-types (3.1)
67
+ mime-types-data (~> 3.2015)
68
+ mime-types-data (3.2016.0521)
69
+ mini_portile2 (2.1.0)
70
+ minitest (5.9.0)
71
+ nokogiri (1.6.8)
72
+ mini_portile2 (~> 2.1.0)
73
+ pkg-config (~> 1.1.7)
74
+ pkg-config (1.1.7)
75
+ pry (0.10.4)
76
+ coderay (~> 1.1.0)
77
+ method_source (~> 0.8.1)
78
+ slop (~> 3.4)
79
+ pry-byebug (3.4.0)
80
+ byebug (~> 9.0)
81
+ pry (~> 0.10)
82
+ rack (1.6.4)
83
+ rack-test (0.6.3)
84
+ rack (>= 1.0)
85
+ rails (4.2.6)
86
+ actionmailer (= 4.2.6)
87
+ actionpack (= 4.2.6)
88
+ actionview (= 4.2.6)
89
+ activejob (= 4.2.6)
90
+ activemodel (= 4.2.6)
91
+ activerecord (= 4.2.6)
92
+ activesupport (= 4.2.6)
93
+ bundler (>= 1.3.0, < 2.0)
94
+ railties (= 4.2.6)
95
+ sprockets-rails
96
+ rails-deprecated_sanitizer (1.0.3)
97
+ activesupport (>= 4.2.0.alpha)
98
+ rails-dom-testing (1.0.7)
99
+ activesupport (>= 4.2.0.beta, < 5.0)
100
+ nokogiri (~> 1.6.0)
101
+ rails-deprecated_sanitizer (>= 1.0.1)
102
+ rails-html-sanitizer (1.0.3)
103
+ loofah (~> 2.0)
104
+ railties (4.2.6)
105
+ actionpack (= 4.2.6)
106
+ activesupport (= 4.2.6)
107
+ rake (>= 0.8.7)
108
+ thor (>= 0.18.1, < 2.0)
109
+ rake (10.5.0)
110
+ rspec (3.5.0)
111
+ rspec-core (~> 3.5.0)
112
+ rspec-expectations (~> 3.5.0)
113
+ rspec-mocks (~> 3.5.0)
114
+ rspec-core (3.5.3)
115
+ rspec-support (~> 3.5.0)
116
+ rspec-expectations (3.5.0)
117
+ diff-lcs (>= 1.2.0, < 2.0)
118
+ rspec-support (~> 3.5.0)
119
+ rspec-mocks (3.5.0)
120
+ diff-lcs (>= 1.2.0, < 2.0)
121
+ rspec-support (~> 3.5.0)
122
+ rspec-rails (3.5.1)
123
+ actionpack (>= 3.0)
124
+ activesupport (>= 3.0)
125
+ railties (>= 3.0)
126
+ rspec-core (~> 3.5.0)
127
+ rspec-expectations (~> 3.5.0)
128
+ rspec-mocks (~> 3.5.0)
129
+ rspec-support (~> 3.5.0)
130
+ rspec-support (3.5.0)
131
+ slop (3.6.0)
132
+ sprockets (3.7.0)
133
+ concurrent-ruby (~> 1.0)
134
+ rack (> 1, < 3)
135
+ sprockets-rails (3.2.0)
136
+ actionpack (>= 4.0)
137
+ activesupport (>= 4.0)
138
+ sprockets (>= 3.0.0)
139
+ stronger_parameters (2.6.0)
140
+ actionpack (>= 3.2, < 5.1)
141
+ thor (0.19.1)
142
+ thread_safe (0.3.5)
143
+ tzinfo (1.2.2)
144
+ thread_safe (~> 0.1)
145
+
146
+ PLATFORMS
147
+ ruby
148
+
149
+ DEPENDENCIES
150
+ appraisal
151
+ bundler (~> 1.11)
152
+ pry
153
+ pry-byebug
154
+ rails (~> 4.1)
155
+ rake (~> 10.0)
156
+ rspec (~> 3.0)
157
+ rspec-rails (~> 3.0)
158
+ strong_resources!
159
+
160
+ BUNDLED WITH
161
+ 1.12.5
@@ -0,0 +1,13 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 5.0"
6
+
7
+ group :test do
8
+ gem "appraisal"
9
+ gem "pry"
10
+ gem "pry-byebug"
11
+ end
12
+
13
+ gemspec :path => "../"
@@ -0,0 +1,165 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ strong_resources (0.1.0)
5
+ rails (>= 4.1, < 6.0)
6
+ stronger_parameters (~> 2.6)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actioncable (5.0.0.1)
12
+ actionpack (= 5.0.0.1)
13
+ nio4r (~> 1.2)
14
+ websocket-driver (~> 0.6.1)
15
+ actionmailer (5.0.0.1)
16
+ actionpack (= 5.0.0.1)
17
+ actionview (= 5.0.0.1)
18
+ activejob (= 5.0.0.1)
19
+ mail (~> 2.5, >= 2.5.4)
20
+ rails-dom-testing (~> 2.0)
21
+ actionpack (5.0.0.1)
22
+ actionview (= 5.0.0.1)
23
+ activesupport (= 5.0.0.1)
24
+ rack (~> 2.0)
25
+ rack-test (~> 0.6.3)
26
+ rails-dom-testing (~> 2.0)
27
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
28
+ actionview (5.0.0.1)
29
+ activesupport (= 5.0.0.1)
30
+ builder (~> 3.1)
31
+ erubis (~> 2.7.0)
32
+ rails-dom-testing (~> 2.0)
33
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
34
+ activejob (5.0.0.1)
35
+ activesupport (= 5.0.0.1)
36
+ globalid (>= 0.3.6)
37
+ activemodel (5.0.0.1)
38
+ activesupport (= 5.0.0.1)
39
+ activerecord (5.0.0.1)
40
+ activemodel (= 5.0.0.1)
41
+ activesupport (= 5.0.0.1)
42
+ arel (~> 7.0)
43
+ activesupport (5.0.0.1)
44
+ concurrent-ruby (~> 1.0, >= 1.0.2)
45
+ i18n (~> 0.7)
46
+ minitest (~> 5.1)
47
+ tzinfo (~> 1.1)
48
+ appraisal (2.1.0)
49
+ bundler
50
+ rake
51
+ thor (>= 0.14.0)
52
+ arel (7.1.1)
53
+ builder (3.2.2)
54
+ byebug (9.0.5)
55
+ coderay (1.1.1)
56
+ concurrent-ruby (1.0.2)
57
+ diff-lcs (1.2.5)
58
+ erubis (2.7.0)
59
+ globalid (0.3.7)
60
+ activesupport (>= 4.1.0)
61
+ i18n (0.7.0)
62
+ loofah (2.0.3)
63
+ nokogiri (>= 1.5.9)
64
+ mail (2.6.4)
65
+ mime-types (>= 1.16, < 4)
66
+ method_source (0.8.2)
67
+ mime-types (3.1)
68
+ mime-types-data (~> 3.2015)
69
+ mime-types-data (3.2016.0521)
70
+ mini_portile2 (2.1.0)
71
+ minitest (5.9.0)
72
+ nio4r (1.2.1)
73
+ nokogiri (1.6.8)
74
+ mini_portile2 (~> 2.1.0)
75
+ pkg-config (~> 1.1.7)
76
+ pkg-config (1.1.7)
77
+ pry (0.10.4)
78
+ coderay (~> 1.1.0)
79
+ method_source (~> 0.8.1)
80
+ slop (~> 3.4)
81
+ pry-byebug (3.4.0)
82
+ byebug (~> 9.0)
83
+ pry (~> 0.10)
84
+ rack (2.0.1)
85
+ rack-test (0.6.3)
86
+ rack (>= 1.0)
87
+ rails (5.0.0.1)
88
+ actioncable (= 5.0.0.1)
89
+ actionmailer (= 5.0.0.1)
90
+ actionpack (= 5.0.0.1)
91
+ actionview (= 5.0.0.1)
92
+ activejob (= 5.0.0.1)
93
+ activemodel (= 5.0.0.1)
94
+ activerecord (= 5.0.0.1)
95
+ activesupport (= 5.0.0.1)
96
+ bundler (>= 1.3.0, < 2.0)
97
+ railties (= 5.0.0.1)
98
+ sprockets-rails (>= 2.0.0)
99
+ rails-dom-testing (2.0.1)
100
+ activesupport (>= 4.2.0, < 6.0)
101
+ nokogiri (~> 1.6.0)
102
+ rails-html-sanitizer (1.0.3)
103
+ loofah (~> 2.0)
104
+ railties (5.0.0.1)
105
+ actionpack (= 5.0.0.1)
106
+ activesupport (= 5.0.0.1)
107
+ method_source
108
+ rake (>= 0.8.7)
109
+ thor (>= 0.18.1, < 2.0)
110
+ rake (10.5.0)
111
+ rspec (3.5.0)
112
+ rspec-core (~> 3.5.0)
113
+ rspec-expectations (~> 3.5.0)
114
+ rspec-mocks (~> 3.5.0)
115
+ rspec-core (3.5.3)
116
+ rspec-support (~> 3.5.0)
117
+ rspec-expectations (3.5.0)
118
+ diff-lcs (>= 1.2.0, < 2.0)
119
+ rspec-support (~> 3.5.0)
120
+ rspec-mocks (3.5.0)
121
+ diff-lcs (>= 1.2.0, < 2.0)
122
+ rspec-support (~> 3.5.0)
123
+ rspec-rails (3.5.1)
124
+ actionpack (>= 3.0)
125
+ activesupport (>= 3.0)
126
+ railties (>= 3.0)
127
+ rspec-core (~> 3.5.0)
128
+ rspec-expectations (~> 3.5.0)
129
+ rspec-mocks (~> 3.5.0)
130
+ rspec-support (~> 3.5.0)
131
+ rspec-support (3.5.0)
132
+ slop (3.6.0)
133
+ sprockets (3.7.0)
134
+ concurrent-ruby (~> 1.0)
135
+ rack (> 1, < 3)
136
+ sprockets-rails (3.2.0)
137
+ actionpack (>= 4.0)
138
+ activesupport (>= 4.0)
139
+ sprockets (>= 3.0.0)
140
+ stronger_parameters (2.6.0)
141
+ actionpack (>= 3.2, < 5.1)
142
+ thor (0.19.1)
143
+ thread_safe (0.3.5)
144
+ tzinfo (1.2.2)
145
+ thread_safe (~> 0.1)
146
+ websocket-driver (0.6.4)
147
+ websocket-extensions (>= 0.1.0)
148
+ websocket-extensions (0.1.2)
149
+
150
+ PLATFORMS
151
+ ruby
152
+
153
+ DEPENDENCIES
154
+ appraisal
155
+ bundler (~> 1.11)
156
+ pry
157
+ pry-byebug
158
+ rails (~> 5.0)
159
+ rake (~> 10.0)
160
+ rspec (~> 3.0)
161
+ rspec-rails (~> 3.0)
162
+ strong_resources!
163
+
164
+ BUNDLED WITH
165
+ 1.12.5
@@ -0,0 +1,48 @@
1
+ require 'stronger_parameters'
2
+
3
+ require "strong_resources/version"
4
+ require "strong_resources/configuration"
5
+ require "strong_resources/strong_resource"
6
+ require "strong_resources/controller/mixin"
7
+
8
+ module StrongResources
9
+ class UnregisteredResource < StandardError
10
+ def initialize(name)
11
+ @name = name
12
+ end
13
+
14
+ def message
15
+ "Resource with name #{@name} not registered"
16
+ end
17
+ end
18
+
19
+ class UnregisteredParam < StandardError
20
+ def initialize(name)
21
+ @name = name
22
+ end
23
+
24
+ def message
25
+ "Parameter with name #{@name} not registered"
26
+ end
27
+ end
28
+
29
+ def self.configure(&blk)
30
+ config.instance_eval(&blk)
31
+ end
32
+
33
+ def self.config
34
+ @config ||= Configuration.new
35
+ end
36
+
37
+ def self.find(name)
38
+ found = config.strong_resources[name]
39
+ raise UnregisteredResource.new(name) unless found
40
+ found
41
+ end
42
+
43
+ def self.type_for_param(name)
44
+ found = config.strong_params[name][:type]
45
+ raise UnregisteredParam.new(name) unless found
46
+ found
47
+ end
48
+ end
@@ -0,0 +1,29 @@
1
+ module StrongResources
2
+ class Configuration
3
+ attr_accessor :strong_params, :strong_resources
4
+
5
+ def initialize
6
+ self.strong_resources = {}
7
+ self.strong_params = {}
8
+ define_default_params
9
+ end
10
+
11
+ def strong_param(name, swagger:, type:)
12
+ self.strong_params[name] = { swagger: swagger, type: type }
13
+ end
14
+
15
+ def strong_resource(name, &blk)
16
+ resource = { name: name, base: blk }
17
+ self.strong_resources[name] = resource
18
+ end
19
+
20
+ private
21
+
22
+ def define_default_params
23
+ strong_param :id, swagger: :string, type: ActionController::Parameters.string
24
+ strong_param :string, swagger: :string, type: ActionController::Parameters.string
25
+ strong_param :integer, swagger: :integer, type: ActionController::Parameters.integer
26
+ strong_param :boolean, swagger: :boolean, type: ActionController::Parameters.boolean
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,41 @@
1
+ module StrongResources
2
+ module Controller
3
+ module Mixin
4
+ def self.included(klass)
5
+ klass.class_eval do
6
+ extend ClassMethods
7
+ class << self
8
+ attr_accessor :_strong_resources
9
+ end
10
+ end
11
+ end
12
+
13
+ def strong_resource
14
+ resource = self.class._strong_resources[action_name.to_sym]
15
+ _params = params
16
+ _params = _params.require(resource.require) if resource.require
17
+ _params.permit(resource.permits(self))
18
+ end
19
+
20
+ def update_action?
21
+ action_name == 'update'
22
+ end
23
+
24
+ module ClassMethods
25
+ def strong_resource(name, opts = {}, &blk)
26
+ opts[:require] ||= name unless opts[:require] == false
27
+ resource = StrongResource.from(name, opts, &blk)
28
+
29
+ resources = { create: resource, update: resource }
30
+ resource.customized_actions.each_pair do |action_name, prc|
31
+ resource = StrongResource.from(name, opts, &blk)
32
+ resource.instance_eval(&prc)
33
+ resources[action_name] = resource
34
+ end
35
+
36
+ self._strong_resources = resources
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,145 @@
1
+ module StrongResources
2
+ class StrongResource
3
+ attr_accessor :attributes, :relations, :require, :relation_type,
4
+ :only,
5
+ :except,
6
+ :delete,
7
+ :destroy
8
+ attr_reader :name, :customized_actions, :jsonapi_type
9
+
10
+ def self.from(name, opts = {}, &blk)
11
+ config = StrongResources.config.strong_resources[name]
12
+ resource = new(name)
13
+ resource.require = opts[:require]
14
+ resource.instance_eval(&config[:base])
15
+ resource.instance_eval(&blk) if blk
16
+ resource
17
+ end
18
+
19
+ def initialize(name)
20
+ @name = name
21
+ @jsonapi_type = name.to_s.pluralize
22
+ @customized_actions = {}
23
+ self.attributes = {}
24
+ self.relations = {}
25
+ end
26
+
27
+ def jsonapi_type(type = nil)
28
+ if type
29
+ @jsonapi_type = type
30
+ else
31
+ @jsonapi_type
32
+ end
33
+ end
34
+
35
+ def delete?
36
+ !!@delete
37
+ end
38
+
39
+ def destroy?
40
+ !!@destroy
41
+ end
42
+
43
+ def attribute(name, type, opts = {})
44
+ self.attributes[name] = { type: type, if: opts[:if] }
45
+ end
46
+
47
+ def remove_attribute(name)
48
+ self.attributes.delete(name)
49
+ end
50
+
51
+ def on(action_name, &blk)
52
+ self.customized_actions[action_name] = blk
53
+ end
54
+
55
+ def has_many?
56
+ relation_type == :has_many
57
+ end
58
+
59
+ def has_many(name,
60
+ resource: nil,
61
+ only: nil,
62
+ except: nil,
63
+ delete: false,
64
+ destroy: false,
65
+ &blk)
66
+ resource_name = resource || name.to_s.singularize.to_sym
67
+ related_resource = self.class.from(resource_name)
68
+ related_resource.instance_eval(&blk) if block_given?
69
+ related_resource.relation_type = :has_many
70
+ add_relation(name, related_resource, only, except, delete, destroy)
71
+ end
72
+
73
+ def belongs_to(name,
74
+ resource: nil,
75
+ only: nil,
76
+ except: nil,
77
+ delete: false,
78
+ destroy: false,
79
+ &blk)
80
+ resource_name = resource || name
81
+ related_resource = self.class.from(resource_name, &blk)
82
+ add_relation(name, related_resource, only, except, delete, destroy)
83
+ end
84
+
85
+ def has_one(*args, &blk)
86
+ belongs_to(*args, &blk)
87
+ end
88
+
89
+ def permits(controller)
90
+ base_permits(self, controller).tap do |permits|
91
+ self.relations.each_pair do |relation_name, opts|
92
+ related_resource = opts[:resource]
93
+ related = related_permits(related_resource, controller)
94
+
95
+ permits.merge!(:"#{relation_name}_attributes" => related)
96
+ end
97
+ end
98
+ end
99
+
100
+ private
101
+
102
+ def base_permits(resource, controller)
103
+ permits = {}
104
+ resource.attributes.each_pair do |name, opts|
105
+ next if (opts[:if] and opts[:if].call(controller) == false)
106
+ permits.merge!(name => StrongResources.type_for_param(opts[:type]))
107
+ end
108
+
109
+ permits = permits.slice(*resource.only) if resource.only
110
+ permits = permits.except(*resource.except) if resource.except
111
+ permits
112
+ end
113
+
114
+ def related_permits(related_resource, controller)
115
+ related_resource.permits(controller).tap do |permits|
116
+ permits.merge!(id: StrongResources.type_for_param(:id))
117
+
118
+ if controller.update_action?
119
+ merge_delete_destroy(related_resource, permits)
120
+ end
121
+ end
122
+ end
123
+
124
+ def merge_delete_destroy(related_resource, permits)
125
+ if related_resource.delete?
126
+ permits.merge!(_delete: StrongResources.type_for_param(:boolean))
127
+ end
128
+
129
+ if related_resource.destroy?
130
+ permits.merge!(_destroy: StrongResources.type_for_param(:boolean))
131
+ end
132
+ end
133
+
134
+ def add_relation(name, resource, only, except, delete, destroy)
135
+ resource.only = only
136
+ resource.except = except
137
+ resource.delete = delete
138
+ resource.destroy = destroy
139
+
140
+ self.relations[name] = {
141
+ resource: resource
142
+ }
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,3 @@
1
+ module StrongResources
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'strong_resources/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "strong_resources"
8
+ spec.version = StrongResources::VERSION
9
+ spec.authors = ["Lee Richmond"]
10
+ spec.email = ["lrichmond1@bloomberg.net"]
11
+
12
+ spec.summary = %q{Auto-generate swagger docs and strong params}
13
+ spec.description = %q{Think factory girl for strong parameters}
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "stronger_parameters", "~> 2.6"
22
+ spec.add_dependency "rails", [">= 4.1", "< 6.0"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.11"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "rspec-rails", "~> 3.0"
28
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: strong_resources
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Lee Richmond
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: stronger_parameters
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '4.1'
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '6.0'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '4.1'
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '6.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.11'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.11'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '10.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '10.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '3.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '3.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rspec-rails
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '3.0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '3.0'
103
+ description: Think factory girl for strong parameters
104
+ email:
105
+ - lrichmond1@bloomberg.net
106
+ executables: []
107
+ extensions: []
108
+ extra_rdoc_files: []
109
+ files:
110
+ - ".gitignore"
111
+ - ".rspec"
112
+ - ".travis.yml"
113
+ - Appraisals
114
+ - Gemfile
115
+ - LICENSE.txt
116
+ - README.md
117
+ - Rakefile
118
+ - bin/console
119
+ - bin/setup
120
+ - gemfiles/rails_4.gemfile
121
+ - gemfiles/rails_4.gemfile.lock
122
+ - gemfiles/rails_5.gemfile
123
+ - gemfiles/rails_5.gemfile.lock
124
+ - lib/strong_resources.rb
125
+ - lib/strong_resources/configuration.rb
126
+ - lib/strong_resources/controller/mixin.rb
127
+ - lib/strong_resources/strong_resource.rb
128
+ - lib/strong_resources/version.rb
129
+ - strong_resources.gemspec
130
+ homepage:
131
+ licenses:
132
+ - MIT
133
+ metadata: {}
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 2.4.5.1
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: Auto-generate swagger docs and strong params
154
+ test_files: []
155
+ has_rdoc: