railjet 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +44 -0
- data/.rspec +2 -0
- data/.ruby-gemset +2 -0
- data/.ruby-version +1 -0
- data/.travis.yml +2 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +96 -0
- data/LICENSE.txt +21 -0
- data/README.md +250 -0
- data/Rakefile +17 -0
- data/VERSION +1 -0
- data/bin/bundler +17 -0
- data/bin/coderay +17 -0
- data/bin/console +14 -0
- data/bin/htmldiff +17 -0
- data/bin/ldiff +17 -0
- data/bin/pry +17 -0
- data/bin/rake +17 -0
- data/bin/rspec +17 -0
- data/bin/setup +8 -0
- data/lib/active_model/merge_errors.rb +22 -0
- data/lib/gettext_i18n/translate_attributes.rb +73 -0
- data/lib/railjet.rb +46 -0
- data/lib/railjet/auth/ability.rb +20 -0
- data/lib/railjet/auth/activity.rb +12 -0
- data/lib/railjet/composed_policy.rb +41 -0
- data/lib/railjet/context.rb +32 -0
- data/lib/railjet/form.rb +10 -0
- data/lib/railjet/policy.rb +26 -0
- data/lib/railjet/railtie.rb +7 -0
- data/lib/railjet/repository.rb +34 -0
- data/lib/railjet/repository/active_record.rb +54 -0
- data/lib/railjet/repository/cupido.rb +34 -0
- data/lib/railjet/repository/registry.rb +71 -0
- data/lib/railjet/use_case.rb +65 -0
- data/lib/railjet/util/form_helper.rb +26 -0
- data/lib/railjet/util/policy_helper.rb +9 -0
- data/lib/railjet/util/use_case_helper.rb +9 -0
- data/lib/railjet/validator.rb +15 -0
- data/lib/railjet/version.rb +3 -0
- data/lib/tasks/gettext_i18n.rake +36 -0
- data/railjet.gemspec +33 -0
- metadata +226 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2589b9af1a383b92da0c3aff67d6ac361fc51fcf
|
4
|
+
data.tar.gz: fe64333bca9ae67477de220278f2fb940903ef1b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f9892bc4c813cf15d1f7d9b313cd06f535901fc1be87c4528e2296916d7a0cb716d1cdd52750175ad0adef42281737b43db49a2791406239dd6caaaef4afbddf
|
7
|
+
data.tar.gz: ba0849ad2d77f54bc19c8dcb6474f161036f2227ab858207a90115fe1590f6ca584e4d5ddf918282a6ad8e317e87f1af6982c6101d3ebbe3d7b7c61e8ed387eb
|
data/.gitignore
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
*.rbc
|
2
|
+
capybara-*.html
|
3
|
+
/log
|
4
|
+
/tmp
|
5
|
+
/db/*.sqlite3
|
6
|
+
/db/*.sqlite3-journal
|
7
|
+
/public/system
|
8
|
+
/coverage/
|
9
|
+
/spec/tmp
|
10
|
+
**.orig
|
11
|
+
rerun.txt
|
12
|
+
pickle-email-*.html
|
13
|
+
|
14
|
+
# TODO Comment out these rules if you are OK with secrets being uploaded to the repo
|
15
|
+
config/initializers/secret_token.rb
|
16
|
+
config/secrets.yml
|
17
|
+
|
18
|
+
# dotenv
|
19
|
+
# TODO Comment out this rule if environment variables can be committed
|
20
|
+
.env
|
21
|
+
|
22
|
+
## Environment normalization:
|
23
|
+
/.bundle
|
24
|
+
/vendor/bundle
|
25
|
+
|
26
|
+
# these should all be checked in to normalize the environment:
|
27
|
+
# Gemfile.lock, .ruby-version, .ruby-gemset
|
28
|
+
|
29
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
30
|
+
.rvmrc
|
31
|
+
|
32
|
+
# if using bower-rails ignore default bower_components path bower.json files
|
33
|
+
/vendor/assets/bower_components
|
34
|
+
*.bowerrc
|
35
|
+
bower.json
|
36
|
+
|
37
|
+
# Ignore pow environment settings
|
38
|
+
.powenv
|
39
|
+
|
40
|
+
# Ignore Byebug command history file.
|
41
|
+
.byebug_history
|
42
|
+
|
43
|
+
# we don't need the gems in the repo
|
44
|
+
/pkg/*.gem
|
data/.rspec
ADDED
data/.ruby-gemset
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.3.3
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
railjet (1.0.1)
|
5
|
+
activemodel (~> 4.2)
|
6
|
+
activesupport (~> 4.2)
|
7
|
+
validates_timeliness (~> 3.0.15)
|
8
|
+
virtus (~> 1.0.2)
|
9
|
+
|
10
|
+
GEM
|
11
|
+
remote: https://rubygems.org/
|
12
|
+
specs:
|
13
|
+
activemodel (4.2.8)
|
14
|
+
activesupport (= 4.2.8)
|
15
|
+
builder (~> 3.1)
|
16
|
+
activesupport (4.2.8)
|
17
|
+
i18n (~> 0.7)
|
18
|
+
minitest (~> 5.1)
|
19
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
20
|
+
tzinfo (~> 1.1)
|
21
|
+
axiom-types (0.1.1)
|
22
|
+
descendants_tracker (~> 0.0.4)
|
23
|
+
ice_nine (~> 0.11.0)
|
24
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
25
|
+
builder (3.2.3)
|
26
|
+
coderay (1.1.1)
|
27
|
+
coercible (1.0.0)
|
28
|
+
descendants_tracker (~> 0.0.1)
|
29
|
+
descendants_tracker (0.0.4)
|
30
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
31
|
+
diff-lcs (1.2.5)
|
32
|
+
equalizer (0.0.11)
|
33
|
+
faraday (0.9.2)
|
34
|
+
multipart-post (>= 1.2, < 3)
|
35
|
+
gemfury (0.6.0)
|
36
|
+
faraday (>= 0.9.0, < 0.10.0.pre)
|
37
|
+
highline (~> 1.6.0)
|
38
|
+
multi_json (~> 1.10)
|
39
|
+
netrc (~> 0.10.0)
|
40
|
+
thor (>= 0.14.0, < 1.0.0.pre)
|
41
|
+
highline (1.6.21)
|
42
|
+
i18n (0.8.1)
|
43
|
+
ice_nine (0.11.2)
|
44
|
+
method_source (0.8.2)
|
45
|
+
minitest (5.10.1)
|
46
|
+
multi_json (1.12.1)
|
47
|
+
multipart-post (2.0.0)
|
48
|
+
netrc (0.10.3)
|
49
|
+
pry (0.10.4)
|
50
|
+
coderay (~> 1.1.0)
|
51
|
+
method_source (~> 0.8.1)
|
52
|
+
slop (~> 3.4)
|
53
|
+
rake (11.3.0)
|
54
|
+
rake-version (1.0.0)
|
55
|
+
rake (~> 11.1)
|
56
|
+
rspec (3.4.0)
|
57
|
+
rspec-core (~> 3.4.0)
|
58
|
+
rspec-expectations (~> 3.4.0)
|
59
|
+
rspec-mocks (~> 3.4.0)
|
60
|
+
rspec-core (3.4.4)
|
61
|
+
rspec-support (~> 3.4.0)
|
62
|
+
rspec-expectations (3.4.0)
|
63
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
64
|
+
rspec-support (~> 3.4.0)
|
65
|
+
rspec-mocks (3.4.1)
|
66
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
67
|
+
rspec-support (~> 3.4.0)
|
68
|
+
rspec-support (3.4.1)
|
69
|
+
slop (3.6.0)
|
70
|
+
thor (0.19.1)
|
71
|
+
thread_safe (0.3.6)
|
72
|
+
timeliness (0.3.8)
|
73
|
+
tzinfo (1.2.2)
|
74
|
+
thread_safe (~> 0.1)
|
75
|
+
validates_timeliness (3.0.15)
|
76
|
+
timeliness (~> 0.3.7)
|
77
|
+
virtus (1.0.5)
|
78
|
+
axiom-types (~> 0.1)
|
79
|
+
coercible (~> 1.0)
|
80
|
+
descendants_tracker (~> 0.0, >= 0.0.3)
|
81
|
+
equalizer (~> 0.0, >= 0.0.9)
|
82
|
+
|
83
|
+
PLATFORMS
|
84
|
+
ruby
|
85
|
+
|
86
|
+
DEPENDENCIES
|
87
|
+
bundler (~> 1.11)
|
88
|
+
gemfury
|
89
|
+
pry
|
90
|
+
railjet!
|
91
|
+
rake (~> 11.0)
|
92
|
+
rake-version (~> 1.0)
|
93
|
+
rspec (~> 3.0)
|
94
|
+
|
95
|
+
BUNDLED WITH
|
96
|
+
1.14.5
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2017 Nedap
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,250 @@
|
|
1
|
+
# Railjet, architecture for high-speed railway [![Build Status](https://travis-ci.com/nedap/railjet.svg?token=sx7KNHQkbW5qxVMs4wKk&branch=master)](https://travis-ci.com/nedap/railjet)
|
2
|
+
|
3
|
+
![Railjet](https://www.swisspasses.com/railpass/popup/railjet/slideshow/RailJet-Zuerich-St.-Anton-Transfer-Ticket-from-Swisspasses.com.jpg)
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'railjet'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install railjet
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Repository
|
24
|
+
|
25
|
+
Assuming your app has 2 ActiveRecord models and uses 1 Cupido object
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
class Employee < ActiveRecord::Base
|
29
|
+
has_many :events
|
30
|
+
end
|
31
|
+
|
32
|
+
class Event < ActiveRecord::Base
|
33
|
+
belongs_to :employee
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
# app/repositories/registry.rb
|
39
|
+
AppRegistry = Railjet::Repository::Registry.new
|
40
|
+
|
41
|
+
AppRegistry.register(:employee, EmployeeRepository, record: Employee, cupido: Cupido::Employee)
|
42
|
+
AppRegistry.register(:event, EventRepository, record: Event)
|
43
|
+
```
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
# app/repositories/employee_repository.rb
|
47
|
+
class EmployeeRepository
|
48
|
+
include Railjet::Repository
|
49
|
+
|
50
|
+
delegate :find_contract, to: :cupido
|
51
|
+
|
52
|
+
def employees_with_contract(employees = record.find_all)
|
53
|
+
employees.select { |e| find_contract(e).present? }
|
54
|
+
end
|
55
|
+
|
56
|
+
class ActiveRecordRepository
|
57
|
+
include Railjet::Repository::ActiveRecord
|
58
|
+
end
|
59
|
+
|
60
|
+
class CupidoRepository
|
61
|
+
include Railjet::Repository::Cupido
|
62
|
+
|
63
|
+
def find_contract(employee)
|
64
|
+
cupido.find(employee.external_id).contract_agreement
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# app/repositories/event_repository.rb
|
70
|
+
class EventRepository
|
71
|
+
include Railjet::Repository
|
72
|
+
|
73
|
+
delegate :employees, to: :registry
|
74
|
+
delegate :find_for_employee, to: :record
|
75
|
+
|
76
|
+
def events_for_active_employees
|
77
|
+
employees.employees_with_contract.map do |e|
|
78
|
+
find_for_employee(e)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
class ActiveRecordRepository
|
83
|
+
include Railjet::Repository::ActiveRecordRepository
|
84
|
+
|
85
|
+
def find_for_employee(employee)
|
86
|
+
record.where(employee_id: employee.id).select(query_columns)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
```
|
91
|
+
|
92
|
+
### UseCase
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
class UseCase::EditEvent
|
96
|
+
include Railjet::UseCase
|
97
|
+
|
98
|
+
# auth module is described below
|
99
|
+
check_ability :can_edit_event?
|
100
|
+
|
101
|
+
# policy module is described below
|
102
|
+
check_policy { |event| policy(EventEmployeeHasContract, event) }
|
103
|
+
|
104
|
+
context :current_employee
|
105
|
+
repositories :events
|
106
|
+
|
107
|
+
def call(id, form)
|
108
|
+
event = events.find_by_id(id)
|
109
|
+
|
110
|
+
with_requirements_check(event) do
|
111
|
+
event.attributes = form.attributes
|
112
|
+
events.persist(event, as: current_employee)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
```
|
117
|
+
|
118
|
+
### Auth
|
119
|
+
```ruby
|
120
|
+
# app/auth/ability.rb
|
121
|
+
class Auth::Ability < Railjet::Auth::Ability
|
122
|
+
|
123
|
+
def can_edit_event?(event)
|
124
|
+
activity(Auth::Event::Edit, event).can_be_edited_by?(actor)
|
125
|
+
end
|
126
|
+
|
127
|
+
def can_take_over_event?(event)
|
128
|
+
activity(Auth::Event::Edit, event).can_be_taken_over_by?(actor)
|
129
|
+
end
|
130
|
+
|
131
|
+
def can_delete_event?(event)
|
132
|
+
activity(Auth::Event::Delete, event).can_be_deleted_by?(actor)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
```
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
# app/auth/event/edit.rb
|
139
|
+
class Auth::Event::Edit < Railjet::Auth::Activity
|
140
|
+
def can_be_edited_by?(actor)
|
141
|
+
end
|
142
|
+
|
143
|
+
def can_be_taken_over_by?(actor)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
# app/auth/event/delete.rb
|
148
|
+
class Auth::Event::Delete < Railjet::Auth::Activity
|
149
|
+
def can_be_deleted_by?(actor)
|
150
|
+
end
|
151
|
+
end
|
152
|
+
```
|
153
|
+
|
154
|
+
### Policy
|
155
|
+
|
156
|
+
```ruby
|
157
|
+
class EventEmployeeHasContract
|
158
|
+
object :event
|
159
|
+
context :repository
|
160
|
+
|
161
|
+
validate :employee_has_contract
|
162
|
+
|
163
|
+
private
|
164
|
+
|
165
|
+
def employee_has_contract
|
166
|
+
errors.add(:employee, "Inactive Employee") unless repository.employees.find_contract(event.employee)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
```
|
170
|
+
|
171
|
+
### Form
|
172
|
+
|
173
|
+
Form is a decorator for request params.
|
174
|
+
In normal Rails app params are just strings, and they're coerced inside ActiveRecord model. We definitely don't want that. We want to coerce and validate as soon as possible.
|
175
|
+
|
176
|
+
Of course, not validations are possible to perform in Form object. It doesn't have access to the context, so there are no settings, no repository. Do as much as possible here, and put more complex stuff in policies
|
177
|
+
|
178
|
+
```ruby
|
179
|
+
class EditForm
|
180
|
+
include Railjet::Form
|
181
|
+
|
182
|
+
attribute :name, String
|
183
|
+
attribute :date, Date
|
184
|
+
attribute :employee_id, Integer
|
185
|
+
|
186
|
+
validates_presence_of :name, :date
|
187
|
+
end
|
188
|
+
```
|
189
|
+
|
190
|
+
### Let's connect everything!
|
191
|
+
|
192
|
+
```ruby
|
193
|
+
# app/context.rb
|
194
|
+
class AppContext < Railjet::Context
|
195
|
+
def initialize(current_employee:, repository:)
|
196
|
+
super
|
197
|
+
end
|
198
|
+
end
|
199
|
+
```
|
200
|
+
|
201
|
+
```ruby
|
202
|
+
# app/controller/events_controller.rb
|
203
|
+
class EventsController < ApplicationController
|
204
|
+
include Railjet::Util::UseCaseHelper
|
205
|
+
include Railjet::Util::FormHelper
|
206
|
+
|
207
|
+
def edit
|
208
|
+
form = form(EditForm) # this will raise exception if validation rules are not met
|
209
|
+
event = use_case(UseCase::EditEvent).call(params[:id], form)
|
210
|
+
|
211
|
+
respond_with event
|
212
|
+
end
|
213
|
+
|
214
|
+
private
|
215
|
+
|
216
|
+
# we need context that will be injected to domain objects
|
217
|
+
def context
|
218
|
+
AppContext.new(
|
219
|
+
current_employee: current_employee,
|
220
|
+
repository: repository
|
221
|
+
)
|
222
|
+
end
|
223
|
+
|
224
|
+
def repository
|
225
|
+
AppRegistry.new(settings: care_provider_settings)
|
226
|
+
end
|
227
|
+
|
228
|
+
def object_params
|
229
|
+
params[:event]
|
230
|
+
end
|
231
|
+
|
232
|
+
# This one is not necessary, but if you need to do something with params
|
233
|
+
# before they go into the form, this is the place:
|
234
|
+
def clean_params
|
235
|
+
dirty_params = super # this calls #object_params
|
236
|
+
dirty_params.select { |k,v| v.present? }
|
237
|
+
end
|
238
|
+
end
|
239
|
+
```
|
240
|
+
|
241
|
+
## Development
|
242
|
+
|
243
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
244
|
+
|
245
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, run rake task to bump the version "rake version:bump:<major,minor,patch>" and then run "rake fury:release" to release it to gemfury
|
246
|
+
|
247
|
+
## Contributing
|
248
|
+
|
249
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/nedap/ons-context.
|
250
|
+
|