respond_with_service 0.0.1 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b9568f5537b49da76900091f13e60ac2f60a0550
4
- data.tar.gz: 34404b4d1155c763c61d0cd978572430e7dda1d0
3
+ metadata.gz: 615c8cf97c2d913c74d400f0db7fc7b2a820c614
4
+ data.tar.gz: 87fb94a1c510b5f0a01f5b7d7418d4ff342542a1
5
5
  SHA512:
6
- metadata.gz: bcd19ea83763ce320474eb8fbe58bd8fe4a8ba94e12af5f08837261c94c0c8a8d6ea1fe6ab9275d92d9652a6e6094506befddcc39217a1390f6daaaaabc07bc9
7
- data.tar.gz: 44f680601cfd67c49fe8f87b910a0117f79b849f79f960d1305d02c5db8807b2b1237b2f92cfa92a812bea2dcaedfc8041f5e3b5b0b81037097d64f68424c95b
6
+ metadata.gz: 2d5952470e6984148cffe18da6005d796bf6987369b040ab5f1abace542264cb2f3bf0232d4341318518de4340a72dc3de408ede3434a06609324099c4d57145
7
+ data.tar.gz: be692bdf2ecfb35382f9521bf764968c33c7531518be70b1b375fe74c427b829decd9ad603858bde4bd9f250546ea7dcc8b30cb129c50160297edc9dd39c3e33
@@ -1,3 +1,3 @@
1
1
  module RespondWithService
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -13,43 +13,56 @@ module AccountModuleService
13
13
  module InstanceMethods
14
14
  private
15
15
 
16
- def redirect_to_after_call service, model_instance, fail_path
17
- if service.call
16
+ def redirect_to_after_call
17
+ if @service_instance.call
18
18
  flash[:notice] = 'Successfully update'
19
- #redirect_to [:admin, :accounts]
20
- redirect_to [fail_path, :admin, model_instance]
19
+ redirect_to [:admin, @model_instance.class.to_s.underscore.pluralize]
21
20
  else
22
21
  # TODO notice errors ap create_account_service.errors
23
- redirect_to [fail_path, :admin, model_instance]
22
+ flash[:error] = 'something went wrong'
23
+ if @fail_path == :new
24
+ redirect_to [@fail_path, :admin, @model_instance.class.to_s.underscore]
25
+ else
26
+ redirect_to [@fail_path, :admin, @model_instance]
27
+ end
24
28
  end
25
29
  end
30
+
31
+ def find_model_instance class_name
32
+ # TODO model can be conf
33
+ model = Object.const_get class_name
34
+ case params[:action].to_sym
35
+ when :create
36
+ @model_instance = model.new
37
+ @fail_path = :new
38
+ when :update
39
+ @model_instance = model.find params[:id]
40
+ @fail_path = :edit
41
+ end
42
+ end
43
+
44
+ def find_service_instance
45
+ service = Object.const_get(@model_instance.class.to_s)
46
+ .const_get "#{params[:action].capitalize}#{@model_instance.class}Service"
47
+ @service_instance = service.new params, @model_instance
48
+ end
26
49
  end
27
50
 
28
51
  module ClassMethods
29
52
  def respond_with_service class_name, conf
30
53
  conf[:actions].each do |action|
31
- # TODO model can be conf
32
- model = Object.const_get class_name
33
- #service = Object.const_get "#{class_name}Service"
34
- define_method action do
35
- case action
36
- when :create
37
- model_instance = model.new
38
- fail_path = :new
39
- service = Object.const_get(class_name)
40
- .const_get "Create#{class_name}Service"
41
- when :update
42
- model_instance = model.find params[:id]
43
- fail_path = :edit
44
- service = Object.const_get(class_name)
45
- .const_get "Update#{class_name}Service"
46
- end
47
- service_instance = service.new params, model_instance
48
- redirect_to_after_call service_instance,
49
- model_instance, fail_path
50
- end
54
+ self.create_action action, class_name
51
55
  end
52
56
  end
57
+
58
+ def create_action action, class_name
59
+ define_method action do
60
+ find_model_instance class_name
61
+ find_service_instance
62
+ redirect_to_after_call
63
+ end
64
+ end
65
+
53
66
  end
54
67
  end
55
68
 
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module .
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: respond_with_service
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pierre-Alexandre Piarulli
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-23 00:00:00.000000000 Z
11
+ date: 2014-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler