light-services 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +2 -2
- data/.rubocop.yml +9 -1152
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -1
- data/.travis.yml +21 -9
- data/Appraisals +6 -1
- data/Gemfile +2 -0
- data/README.md +109 -6
- data/Rakefile +2 -0
- data/bin/console +1 -0
- data/gemfiles/rails_4_0.gemfile +1 -1
- data/gemfiles/rails_4_0.gemfile.lock +2 -2
- data/gemfiles/rails_4_1.gemfile +1 -1
- data/gemfiles/rails_4_1.gemfile.lock +2 -2
- data/gemfiles/rails_4_2.gemfile +1 -1
- data/gemfiles/rails_4_2.gemfile.lock +2 -2
- data/gemfiles/rails_5_0.gemfile +2 -2
- data/gemfiles/rails_5_0.gemfile.lock +80 -86
- data/gemfiles/rails_5_1.gemfile +7 -0
- data/gemfiles/rails_5_1.gemfile.lock +150 -0
- data/lib/light/services/base.rb +5 -4
- data/lib/light/services/callbacks.rb +18 -15
- data/lib/light/services/exceptions.rb +2 -0
- data/lib/light/services/messages.rb +9 -5
- data/lib/light/services/outputs.rb +64 -0
- data/lib/light/services/parameters.rb +40 -50
- data/lib/light/services/version.rb +3 -1
- data/lib/light/services.rb +3 -1
- data/light-services.gemspec +2 -0
- metadata +7 -4
- data/lib/light/services/variables.rb +0 -38
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
light-services
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.4.1
|
data/.travis.yml
CHANGED
@@ -1,17 +1,29 @@
|
|
1
|
+
env:
|
2
|
+
global:
|
3
|
+
- CC_TEST_REPORTER_ID=dedf6049120e32b166263dd9e07b2abea3991a18b2d0c8dc86792776ca7578ce
|
4
|
+
- GIT_COMMITTED_AT=$(if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then git log -1 --pretty=format:%ct; else git log -1 --skip 1 --pretty=format:%ct; fi)
|
1
5
|
sudo: false
|
2
6
|
language: ruby
|
3
|
-
|
4
7
|
rvm:
|
5
|
-
- 2.
|
6
|
-
|
7
|
-
|
8
|
-
|
8
|
+
- 2.4.1
|
9
|
+
before_script:
|
10
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
11
|
+
- chmod +x ./cc-test-reporter
|
12
|
+
- ./cc-test-reporter before-build
|
13
|
+
before_install: gem install bundler
|
9
14
|
gemfile:
|
10
15
|
- gemfiles/rails_4_0.gemfile
|
11
16
|
- gemfiles/rails_4_1.gemfile
|
12
17
|
- gemfiles/rails_4_2.gemfile
|
13
18
|
- gemfiles/rails_5_0.gemfile
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
19
|
+
- gemfiles/rails_5_1.gemfile
|
20
|
+
script:
|
21
|
+
- bundle exec rake
|
22
|
+
after_script:
|
23
|
+
# Preferably you will run test-reporter on branch update events. But
|
24
|
+
# if you setup travis to build PR updates only, you don't need to run
|
25
|
+
# the line below
|
26
|
+
- if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT; fi
|
27
|
+
# In the case where travis is setup to build PR updates only,
|
28
|
+
# uncomment the line below
|
29
|
+
# - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
|
data/Appraisals
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
appraise 'rails-4-0' do
|
2
4
|
gem 'rails', '4.0.13'
|
3
5
|
end
|
@@ -11,6 +13,9 @@ appraise 'rails-4-2' do
|
|
11
13
|
end
|
12
14
|
|
13
15
|
appraise 'rails-5-0' do
|
14
|
-
gem 'rails', '5.0.
|
16
|
+
gem 'rails', '5.0.5'
|
15
17
|
end
|
16
18
|
|
19
|
+
appraise 'rails-5-1' do
|
20
|
+
gem 'rails', '5.1.3'
|
21
|
+
end
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -4,18 +4,18 @@
|
|
4
4
|
[![Code Climate](https://codeclimate.com/github/light-ruby/light-services/badges/gpa.svg)](https://codeclimate.com/github/light-ruby/light-services)
|
5
5
|
[![Test Coverage](https://codeclimate.com/github/light-ruby/light-services/badges/coverage.svg)](https://codeclimate.com/github/light-ruby/light-services/coverage)
|
6
6
|
|
7
|
-
|
7
|
+
Implementation of Service Object Pattern for Ruby/Rails. Compatible with Rails 5.1 and 5.0, 4.2, 4.1, 4.0.
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
Service Object Pattern – What is it? Check it here:
|
10
|
+
- [Wikipedia](https://en.wikipedia.org/wiki/Service_layer_pattern)
|
11
|
+
- [Essential RubyOnRails patterns — part 1: Service Objects](https://medium.com/selleo/essential-rubyonrails-patterns-part-1-service-objects-1af9f9573ca1)
|
12
12
|
|
13
13
|
## Installation
|
14
14
|
|
15
15
|
Add this line to your application's Gemfile:
|
16
16
|
|
17
17
|
```ruby
|
18
|
-
gem 'light-services', '~> 0.
|
18
|
+
gem 'light-services', '~> 0.5'
|
19
19
|
```
|
20
20
|
|
21
21
|
And then execute:
|
@@ -28,7 +28,110 @@ Or install it yourself as:
|
|
28
28
|
|
29
29
|
## Usage
|
30
30
|
|
31
|
-
|
31
|
+
#### Examples of usage:
|
32
|
+
|
33
|
+
**Change state of the record:**
|
34
|
+
```ruby
|
35
|
+
class Painting::Publish < ApplicationService
|
36
|
+
# Parameters
|
37
|
+
param :painting, type: Painting
|
38
|
+
param :user, type: User
|
39
|
+
|
40
|
+
# Callbacks
|
41
|
+
before :authorize
|
42
|
+
after :send_email_notification
|
43
|
+
|
44
|
+
def run
|
45
|
+
painting.publish!
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def authorize
|
51
|
+
pundit_authorize!(painting, user, :publish?)
|
52
|
+
end
|
53
|
+
|
54
|
+
def send_email_notification
|
55
|
+
PaintingMailer.publish_email(painting).deliver_now
|
56
|
+
end
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
**Create a new record:**
|
61
|
+
```ruby
|
62
|
+
class Owner::Create < ApplicationService
|
63
|
+
# Parameters
|
64
|
+
param :params, type: ActionController::Parameters
|
65
|
+
param :user, type: User
|
66
|
+
|
67
|
+
# Outputs
|
68
|
+
output :owner
|
69
|
+
|
70
|
+
# Callbacks
|
71
|
+
before :assign_attributes
|
72
|
+
before :authorize
|
73
|
+
before :validate
|
74
|
+
|
75
|
+
def run
|
76
|
+
owner.save!
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
def assign_attributes
|
82
|
+
self.owner = Owner.new(owner_params)
|
83
|
+
end
|
84
|
+
|
85
|
+
def authorize
|
86
|
+
pundit_authorize!(owner, user, :create?)
|
87
|
+
end
|
88
|
+
|
89
|
+
def validate
|
90
|
+
return if owner.valid?
|
91
|
+
errors.from_record(owner)
|
92
|
+
end
|
93
|
+
|
94
|
+
def owner_params
|
95
|
+
params
|
96
|
+
.require(:owner)
|
97
|
+
.permit(:name)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
```
|
101
|
+
|
102
|
+
**Integration with Pundit:**
|
103
|
+
```ruby
|
104
|
+
class ApplicationService < Light::Services::Base
|
105
|
+
def pundit_authorize!(record, user, action = nil)
|
106
|
+
action = convert_action(action, :show?)
|
107
|
+
policy = Pundit.policy!(user, record)
|
108
|
+
|
109
|
+
return true if policy.public_send(action)
|
110
|
+
|
111
|
+
Rails.logger.info "Pundit: not allowed to #{action} this #{record.inspect}"
|
112
|
+
raise Pundit::NotAuthorizedError, query: action, policy: policy, record: record
|
113
|
+
end
|
114
|
+
|
115
|
+
def pundit_scope!(scope, user, action = nil)
|
116
|
+
action = convert_action(action, :index?)
|
117
|
+
pundit_authorize!(scope, user, action)
|
118
|
+
|
119
|
+
Pundit.policy_scope!(user, scope)
|
120
|
+
end
|
121
|
+
|
122
|
+
private
|
123
|
+
|
124
|
+
def convert_action(action, default)
|
125
|
+
action = action.to_s
|
126
|
+
|
127
|
+
return default if action.blank?
|
128
|
+
return action if action.ends_with?('?')
|
129
|
+
|
130
|
+
action + '?'
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
```
|
32
135
|
|
33
136
|
## Development
|
34
137
|
|
data/Rakefile
CHANGED
data/bin/console
CHANGED
data/gemfiles/rails_4_0.gemfile
CHANGED
data/gemfiles/rails_4_1.gemfile
CHANGED
data/gemfiles/rails_4_2.gemfile
CHANGED
data/gemfiles/rails_5_0.gemfile
CHANGED
@@ -1,141 +1,135 @@
|
|
1
1
|
PATH
|
2
2
|
remote: ..
|
3
3
|
specs:
|
4
|
-
light-services (0.
|
4
|
+
light-services (0.5.0)
|
5
5
|
rails (> 4.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
actioncable (5.0.
|
11
|
-
actionpack (= 5.0.
|
12
|
-
nio4r (
|
10
|
+
actioncable (5.0.5)
|
11
|
+
actionpack (= 5.0.5)
|
12
|
+
nio4r (>= 1.2, < 3.0)
|
13
13
|
websocket-driver (~> 0.6.1)
|
14
|
-
actionmailer (5.0.
|
15
|
-
actionpack (= 5.0.
|
16
|
-
actionview (= 5.0.
|
17
|
-
activejob (= 5.0.
|
14
|
+
actionmailer (5.0.5)
|
15
|
+
actionpack (= 5.0.5)
|
16
|
+
actionview (= 5.0.5)
|
17
|
+
activejob (= 5.0.5)
|
18
18
|
mail (~> 2.5, >= 2.5.4)
|
19
|
-
rails-dom-testing (~>
|
20
|
-
actionpack (5.0.
|
21
|
-
actionview (= 5.0.
|
22
|
-
activesupport (= 5.0.
|
23
|
-
rack (~> 2.
|
19
|
+
rails-dom-testing (~> 2.0)
|
20
|
+
actionpack (5.0.5)
|
21
|
+
actionview (= 5.0.5)
|
22
|
+
activesupport (= 5.0.5)
|
23
|
+
rack (~> 2.0)
|
24
24
|
rack-test (~> 0.6.3)
|
25
|
-
rails-dom-testing (~>
|
25
|
+
rails-dom-testing (~> 2.0)
|
26
26
|
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
27
|
-
actionview (5.0.
|
28
|
-
activesupport (= 5.0.
|
27
|
+
actionview (5.0.5)
|
28
|
+
activesupport (= 5.0.5)
|
29
29
|
builder (~> 3.1)
|
30
30
|
erubis (~> 2.7.0)
|
31
|
-
rails-dom-testing (~>
|
32
|
-
rails-html-sanitizer (~> 1.0, >= 1.0.
|
33
|
-
activejob (5.0.
|
34
|
-
activesupport (= 5.0.
|
31
|
+
rails-dom-testing (~> 2.0)
|
32
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
33
|
+
activejob (5.0.5)
|
34
|
+
activesupport (= 5.0.5)
|
35
35
|
globalid (>= 0.3.6)
|
36
|
-
activemodel (5.0.
|
37
|
-
activesupport (= 5.0.
|
38
|
-
activerecord (5.0.
|
39
|
-
activemodel (= 5.0.
|
40
|
-
activesupport (= 5.0.
|
36
|
+
activemodel (5.0.5)
|
37
|
+
activesupport (= 5.0.5)
|
38
|
+
activerecord (5.0.5)
|
39
|
+
activemodel (= 5.0.5)
|
40
|
+
activesupport (= 5.0.5)
|
41
41
|
arel (~> 7.0)
|
42
|
-
activesupport (5.0.
|
42
|
+
activesupport (5.0.5)
|
43
43
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
44
44
|
i18n (~> 0.7)
|
45
45
|
minitest (~> 5.1)
|
46
46
|
tzinfo (~> 1.1)
|
47
|
-
appraisal (2.
|
47
|
+
appraisal (2.2.0)
|
48
48
|
bundler
|
49
49
|
rake
|
50
50
|
thor (>= 0.14.0)
|
51
|
-
arel (7.
|
52
|
-
builder (3.2.
|
53
|
-
codeclimate-test-reporter (0.
|
54
|
-
simplecov (
|
55
|
-
concurrent-ruby (1.0.
|
56
|
-
diff-lcs (1.
|
51
|
+
arel (7.1.4)
|
52
|
+
builder (3.2.3)
|
53
|
+
codeclimate-test-reporter (1.0.8)
|
54
|
+
simplecov (<= 0.13)
|
55
|
+
concurrent-ruby (1.0.5)
|
56
|
+
diff-lcs (1.3)
|
57
57
|
docile (1.1.5)
|
58
58
|
erubis (2.7.0)
|
59
|
-
globalid (0.
|
60
|
-
activesupport (>= 4.
|
61
|
-
i18n (0.
|
59
|
+
globalid (0.4.0)
|
60
|
+
activesupport (>= 4.2.0)
|
61
|
+
i18n (0.8.6)
|
62
62
|
json (1.8.6)
|
63
63
|
loofah (2.0.3)
|
64
64
|
nokogiri (>= 1.5.9)
|
65
|
-
mail (2.6.
|
65
|
+
mail (2.6.6)
|
66
66
|
mime-types (>= 1.16, < 4)
|
67
67
|
method_source (0.8.2)
|
68
68
|
mime-types (3.1)
|
69
69
|
mime-types-data (~> 3.2015)
|
70
70
|
mime-types-data (3.2016.0521)
|
71
|
-
mini_portile2 (2.
|
72
|
-
minitest (5.
|
73
|
-
nio4r (
|
74
|
-
nokogiri (1.
|
75
|
-
mini_portile2 (~> 2.
|
76
|
-
|
77
|
-
pkg-config (1.1.7)
|
78
|
-
rack (2.0.0.rc1)
|
79
|
-
json
|
71
|
+
mini_portile2 (2.2.0)
|
72
|
+
minitest (5.10.3)
|
73
|
+
nio4r (2.1.0)
|
74
|
+
nokogiri (1.8.0)
|
75
|
+
mini_portile2 (~> 2.2.0)
|
76
|
+
rack (2.0.3)
|
80
77
|
rack-test (0.6.3)
|
81
78
|
rack (>= 1.0)
|
82
|
-
rails (5.0.
|
83
|
-
actioncable (= 5.0.
|
84
|
-
actionmailer (= 5.0.
|
85
|
-
actionpack (= 5.0.
|
86
|
-
actionview (= 5.0.
|
87
|
-
activejob (= 5.0.
|
88
|
-
activemodel (= 5.0.
|
89
|
-
activerecord (= 5.0.
|
90
|
-
activesupport (= 5.0.
|
91
|
-
bundler (>= 1.3.0
|
92
|
-
railties (= 5.0.
|
79
|
+
rails (5.0.5)
|
80
|
+
actioncable (= 5.0.5)
|
81
|
+
actionmailer (= 5.0.5)
|
82
|
+
actionpack (= 5.0.5)
|
83
|
+
actionview (= 5.0.5)
|
84
|
+
activejob (= 5.0.5)
|
85
|
+
activemodel (= 5.0.5)
|
86
|
+
activerecord (= 5.0.5)
|
87
|
+
activesupport (= 5.0.5)
|
88
|
+
bundler (>= 1.3.0)
|
89
|
+
railties (= 5.0.5)
|
93
90
|
sprockets-rails (>= 2.0.0)
|
94
|
-
rails-
|
95
|
-
activesupport (>= 4.2.0
|
96
|
-
|
97
|
-
activesupport (>= 4.2.0.beta, < 5.0)
|
98
|
-
nokogiri (~> 1.6.0)
|
99
|
-
rails-deprecated_sanitizer (>= 1.0.1)
|
91
|
+
rails-dom-testing (2.0.3)
|
92
|
+
activesupport (>= 4.2.0)
|
93
|
+
nokogiri (>= 1.6)
|
100
94
|
rails-html-sanitizer (1.0.3)
|
101
95
|
loofah (~> 2.0)
|
102
|
-
railties (5.0.
|
103
|
-
actionpack (= 5.0.
|
104
|
-
activesupport (= 5.0.
|
96
|
+
railties (5.0.5)
|
97
|
+
actionpack (= 5.0.5)
|
98
|
+
activesupport (= 5.0.5)
|
105
99
|
method_source
|
106
100
|
rake (>= 0.8.7)
|
107
101
|
thor (>= 0.18.1, < 2.0)
|
108
102
|
rake (10.5.0)
|
109
|
-
rspec (3.
|
110
|
-
rspec-core (~> 3.
|
111
|
-
rspec-expectations (~> 3.
|
112
|
-
rspec-mocks (~> 3.
|
113
|
-
rspec-core (3.
|
114
|
-
rspec-support (~> 3.
|
115
|
-
rspec-expectations (3.
|
103
|
+
rspec (3.6.0)
|
104
|
+
rspec-core (~> 3.6.0)
|
105
|
+
rspec-expectations (~> 3.6.0)
|
106
|
+
rspec-mocks (~> 3.6.0)
|
107
|
+
rspec-core (3.6.0)
|
108
|
+
rspec-support (~> 3.6.0)
|
109
|
+
rspec-expectations (3.6.0)
|
116
110
|
diff-lcs (>= 1.2.0, < 2.0)
|
117
|
-
rspec-support (~> 3.
|
118
|
-
rspec-mocks (3.
|
111
|
+
rspec-support (~> 3.6.0)
|
112
|
+
rspec-mocks (3.6.0)
|
119
113
|
diff-lcs (>= 1.2.0, < 2.0)
|
120
|
-
rspec-support (~> 3.
|
121
|
-
rspec-support (3.
|
114
|
+
rspec-support (~> 3.6.0)
|
115
|
+
rspec-support (3.6.0)
|
122
116
|
simplecov (0.11.2)
|
123
117
|
docile (~> 1.1.0)
|
124
118
|
json (~> 1.8)
|
125
119
|
simplecov-html (~> 0.10.0)
|
126
|
-
simplecov-html (0.10.
|
127
|
-
sprockets (3.
|
120
|
+
simplecov-html (0.10.2)
|
121
|
+
sprockets (3.7.1)
|
128
122
|
concurrent-ruby (~> 1.0)
|
129
123
|
rack (> 1, < 3)
|
130
|
-
sprockets-rails (3.0
|
124
|
+
sprockets-rails (3.2.0)
|
131
125
|
actionpack (>= 4.0)
|
132
126
|
activesupport (>= 4.0)
|
133
127
|
sprockets (>= 3.0.0)
|
134
|
-
thor (0.
|
135
|
-
thread_safe (0.3.
|
136
|
-
tzinfo (1.2.
|
128
|
+
thor (0.20.0)
|
129
|
+
thread_safe (0.3.6)
|
130
|
+
tzinfo (1.2.3)
|
137
131
|
thread_safe (~> 0.1)
|
138
|
-
websocket-driver (0.6.
|
132
|
+
websocket-driver (0.6.5)
|
139
133
|
websocket-extensions (>= 0.1.0)
|
140
134
|
websocket-extensions (0.1.2)
|
141
135
|
|
@@ -147,10 +141,10 @@ DEPENDENCIES
|
|
147
141
|
bundler (~> 1.12)
|
148
142
|
codeclimate-test-reporter
|
149
143
|
light-services!
|
150
|
-
rails (= 5.0.
|
144
|
+
rails (= 5.0.5)
|
151
145
|
rake (~> 10.0)
|
152
146
|
rspec (~> 3.0)
|
153
147
|
simplecov (~> 0.11.2)
|
154
148
|
|
155
149
|
BUNDLED WITH
|
156
|
-
1.
|
150
|
+
1.15.4
|