-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathservices.rb
59 lines (41 loc) · 984 Bytes
/
services.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
module Service
extend ActiveSupport::Concern
included do
attr_reader :params
end
class_methods do
def call(*args)
new(*args).call
end
end
def initialize(params = {})
@params = params
params.each do |key, value|
next if respond_to?(key, true)
define_singleton_method(key) { value }
end
end
end
module Groups
class Create
include Service
def call
#do_something
end
end
end
def call
group = Group.new attributes
contract = GroupContract.new(group)
fail ValidationError.new(contract) unless contract.validate
album.save
album
end
@form = SongForm.new(Song.new)
So you need to know your entity. Where the initialization of the entity should be done on the service
At the end I return the album to avoid the true/false pb and when I create an object I like to have it in response.
def create
Groups::Create.call attributes: group_attributes
rescue ValidationError
render :new
end