light-services 0.4.0 → 0.5.0

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.
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ light-services
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.3.3
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.3.3
6
-
7
- before_install: gem install bundler -v 1.12.5
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
- addons:
16
- code_climate:
17
- repo_token: dedf6049120e32b166263dd9e07b2abea3991a18b2d0c8dc86792776ca7578ce
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.0.rc1'
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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in light-services.gemspec
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
- # WARNING! GEM IN DEVELOPMENT!
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
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/light/service`. To experiment with that code, run `bin/console` for an interactive prompt.
10
-
11
- TODO: Delete this and the text above, and describe your gem
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.4'
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
- TODO: Write usage instructions here
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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bundler/gem_tasks'
2
4
  require 'rspec/core/rake_task'
3
5
 
data/bin/console CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'bundler/setup'
4
5
  require 'light/services'
@@ -4,4 +4,4 @@ source "https://rubygems.org"
4
4
 
5
5
  gem "rails", "4.0.13"
6
6
 
7
- gemspec :path => "../"
7
+ gemspec path: "../"
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- light-services (0.4.0)
4
+ light-services (0.5.0)
5
5
  rails (> 4.0)
6
6
 
7
7
  GEM
@@ -112,4 +112,4 @@ DEPENDENCIES
112
112
  simplecov (~> 0.11.2)
113
113
 
114
114
  BUNDLED WITH
115
- 1.14.4
115
+ 1.15.4
@@ -4,4 +4,4 @@ source "https://rubygems.org"
4
4
 
5
5
  gem "rails", "4.1.15"
6
6
 
7
- gemspec :path => "../"
7
+ gemspec path: "../"
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- light-services (0.4.0)
4
+ light-services (0.5.0)
5
5
  rails (> 4.0)
6
6
 
7
7
  GEM
@@ -116,4 +116,4 @@ DEPENDENCIES
116
116
  simplecov (~> 0.11.2)
117
117
 
118
118
  BUNDLED WITH
119
- 1.14.4
119
+ 1.15.4
@@ -4,4 +4,4 @@ source "https://rubygems.org"
4
4
 
5
5
  gem "rails", "4.2.6"
6
6
 
7
- gemspec :path => "../"
7
+ gemspec path: "../"
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- light-services (0.4.0)
4
+ light-services (0.5.0)
5
5
  rails (> 4.0)
6
6
 
7
7
  GEM
@@ -143,4 +143,4 @@ DEPENDENCIES
143
143
  simplecov (~> 0.11.2)
144
144
 
145
145
  BUNDLED WITH
146
- 1.14.4
146
+ 1.15.4
@@ -2,6 +2,6 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "rails", "5.0.0.rc1"
5
+ gem "rails", "5.0.5"
6
6
 
7
- gemspec :path => "../"
7
+ gemspec path: "../"
@@ -1,141 +1,135 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- light-services (0.4.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.0.rc1)
11
- actionpack (= 5.0.0.rc1)
12
- nio4r (~> 1.2)
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.0.rc1)
15
- actionpack (= 5.0.0.rc1)
16
- actionview (= 5.0.0.rc1)
17
- activejob (= 5.0.0.rc1)
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 (~> 1.0, >= 1.0.5)
20
- actionpack (5.0.0.rc1)
21
- actionview (= 5.0.0.rc1)
22
- activesupport (= 5.0.0.rc1)
23
- rack (~> 2.x)
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 (~> 1.0, >= 1.0.5)
25
+ rails-dom-testing (~> 2.0)
26
26
  rails-html-sanitizer (~> 1.0, >= 1.0.2)
27
- actionview (5.0.0.rc1)
28
- activesupport (= 5.0.0.rc1)
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 (~> 1.0, >= 1.0.5)
32
- rails-html-sanitizer (~> 1.0, >= 1.0.2)
33
- activejob (5.0.0.rc1)
34
- activesupport (= 5.0.0.rc1)
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.0.rc1)
37
- activesupport (= 5.0.0.rc1)
38
- activerecord (5.0.0.rc1)
39
- activemodel (= 5.0.0.rc1)
40
- activesupport (= 5.0.0.rc1)
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.0.rc1)
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.1.0)
47
+ appraisal (2.2.0)
48
48
  bundler
49
49
  rake
50
50
  thor (>= 0.14.0)
51
- arel (7.0.0)
52
- builder (3.2.2)
53
- codeclimate-test-reporter (0.5.1)
54
- simplecov (>= 0.7.1, < 1.0.0)
55
- concurrent-ruby (1.0.2)
56
- diff-lcs (1.2.5)
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.3.6)
60
- activesupport (>= 4.1.0)
61
- i18n (0.7.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.4)
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.1.0)
72
- minitest (5.9.0)
73
- nio4r (1.2.1)
74
- nokogiri (1.6.8)
75
- mini_portile2 (~> 2.1.0)
76
- pkg-config (~> 1.1.7)
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.0.rc1)
83
- actioncable (= 5.0.0.rc1)
84
- actionmailer (= 5.0.0.rc1)
85
- actionpack (= 5.0.0.rc1)
86
- actionview (= 5.0.0.rc1)
87
- activejob (= 5.0.0.rc1)
88
- activemodel (= 5.0.0.rc1)
89
- activerecord (= 5.0.0.rc1)
90
- activesupport (= 5.0.0.rc1)
91
- bundler (>= 1.3.0, < 2.0)
92
- railties (= 5.0.0.rc1)
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-deprecated_sanitizer (1.0.3)
95
- activesupport (>= 4.2.0.alpha)
96
- rails-dom-testing (1.0.7)
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.0.rc1)
103
- actionpack (= 5.0.0.rc1)
104
- activesupport (= 5.0.0.rc1)
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.4.0)
110
- rspec-core (~> 3.4.0)
111
- rspec-expectations (~> 3.4.0)
112
- rspec-mocks (~> 3.4.0)
113
- rspec-core (3.4.4)
114
- rspec-support (~> 3.4.0)
115
- rspec-expectations (3.4.0)
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.4.0)
118
- rspec-mocks (3.4.1)
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.4.0)
121
- rspec-support (3.4.1)
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.0)
127
- sprockets (3.6.1)
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.4)
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.19.1)
135
- thread_safe (0.3.5)
136
- tzinfo (1.2.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.4)
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.0.rc1)
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.14.4
150
+ 1.15.4