rails-add_ons 1.5.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a60eef5c38dafca32f4fdaf89ba17d77a5a63381
4
- data.tar.gz: a9db57320510f42ddca6f617296fc1fc9a3279f1
3
+ metadata.gz: 69a1ab049ebb2b6cb5ffeaf53cf556dcb4d47087
4
+ data.tar.gz: '0683101275419fdf2c44f61aa469a9564885d749'
5
5
  SHA512:
6
- metadata.gz: 6b4a5ad94861003cac7785b22b922c2d38f6e2eac8a8558bdcb3925edbf3a033b78cdcb66b475ad24cc147f56850c74d1bff45670e29cfe0268f745f3844447f
7
- data.tar.gz: cca30dcaf35e43d2bbaf60428c67a05b916e7045c6967e2a4ab380cb3eb0c6d9318159c0a9c0e194a94e53a31649daf9ca6316a7c4f1b3f055ea8f9df0344750
6
+ metadata.gz: 440300dfce19bda1377a4b20496eb1a3370aeb0aa50c90355ed68adfbeaf12a089f7691932a44b1af8579555959d7cf152c29f38cd1ef92583d0ca46c64313b9
7
+ data.tar.gz: d512ac00d69956e12f81c7630351b7ddd1aa3e3f90eac1eaf23074d52cec4cd8bac9ba0a2c92306d7d8adf52a8844c0e5672a307ab8686e194a3bcca371f750c
data/README.md CHANGED
@@ -20,6 +20,10 @@ You may want to add
20
20
 
21
21
  to your applications javascript include file, as it was removed.
22
22
 
23
+ ### Update path to 2.0.0
24
+
25
+ ServiceController::Base now uses the verbs new and create instead of invoke and call. Furthermore @resource is dropped in favor of @service. Please modify your controllers and views and routing accordingly.
26
+
23
27
  ## Installation
24
28
  Add this line to your application's Gemfile:
25
29
 
@@ -12,6 +12,16 @@ module Component
12
12
  @rows[name] = options
13
13
  end
14
14
 
15
+ def timestamps(options = {})
16
+ row(:created_at, options)
17
+ row(:updated_at, options)
18
+ end
19
+
20
+ def association(name, options = {}, &block)
21
+ options.reverse_merge!(block: block) if block_given?
22
+ @rows[name] = options
23
+ end
24
+
15
25
  private
16
26
 
17
27
  def view_locals
@@ -22,4 +32,4 @@ module Component
22
32
  }
23
33
  end
24
34
  end
25
- end
35
+ end
@@ -31,17 +31,29 @@ module ResourcesController::RestActions
31
31
  else
32
32
  @resource.update(permitted_params)
33
33
  end
34
- respond_with(respond_with_namespace, @resource)
34
+ if respond_to?(:after_update_location, true)
35
+ respond_with(respond_with_namespace, @resource, location: after_update_location)
36
+ else
37
+ respond_with(respond_with_namespace, @resource)
38
+ end
35
39
  end
36
40
 
37
41
  def destroy
38
42
  @resource.destroy
39
- respond_with(respond_with_namespace, @resource)
43
+ if respond_to?(:after_destroy_location, true)
44
+ respond_with(respond_with_namespace, @resource, location: after_destroy_location)
45
+ else
46
+ respond_with(respond_with_namespace, @resource)
47
+ end
40
48
  end
41
49
 
42
50
  def create
43
51
  @resource.save
44
- respond_with(respond_with_namespace, @resource)
52
+ if respond_to?(:after_create_location, true)
53
+ respond_with(respond_with_namespace, @resource, location: after_create_location)
54
+ else
55
+ respond_with(respond_with_namespace, @resource)
56
+ end
45
57
  end
46
58
 
47
59
  private
@@ -50,10 +62,6 @@ module ResourcesController::RestActions
50
62
  nil
51
63
  end
52
64
 
53
- def after_create_location
54
- ->(controller) { resource_path(@resource) }
55
- end
56
-
57
65
  def load_collection_scope
58
66
  resource_class
59
67
  end
@@ -0,0 +1,31 @@
1
+ module ServiceController::LocationHistory
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ if respond_to?(:before_action)
6
+ before_action :store_location
7
+ else
8
+ before_filter :store_location
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def store_location
15
+ truncate_location_history(9)
16
+ location_history[Time.zone.now] = request.referer
17
+ end
18
+
19
+ def location_history
20
+ session[:location_history] ||= {}
21
+ end
22
+
23
+ def last_location
24
+ location_history.sort.last.try(:last)
25
+ end
26
+
27
+ def truncate_location_history(count = 0)
28
+ return if location_history.size <= count
29
+ session[:location_history] = session[:location_history].sort.last(count).to_h
30
+ end
31
+ end
@@ -6,40 +6,40 @@ module ServiceController::RestActions
6
6
  responders :flash
7
7
 
8
8
  if respond_to?(:before_action)
9
- before_action :initialize_service_for_invoke, only: [:invoke]
10
- before_action :initialize_service_for_call, only: [:call]
9
+ before_action :initialize_service, only: [:new]
10
+ before_action :initialize_service_for_create, only: [:create]
11
11
  else
12
- before_filter :initialize_service_for_invoke, only: [:invoke]
13
- before_filter :initialize_service_for_call, only: [:call]
12
+ before_filter :initialize_service, only: [:new]
13
+ before_filter :initialize_service_for_create, only: [:create]
14
14
  end
15
15
  end
16
16
 
17
- def index
18
- end
17
+ def new; end
19
18
 
20
- def invoke
21
- respond_with @service
19
+ def create
20
+ perform
22
21
  end
23
22
 
24
- def call
23
+ private
24
+
25
+ def perform
25
26
  @result = execute_service
26
27
  if @result.success?
27
28
 
28
- if respond_to?(:after_success_location)
29
+ if respond_to?(:after_success_location, true)
29
30
  redirect_to(after_success_location, notice: success_message)
30
31
  else
31
32
  flash.now[:success] = success_message
32
- render :success
33
+ render :create
33
34
  end
34
35
  else
35
- render :invoke
36
+ render :new
36
37
  end
37
38
  end
38
39
 
39
- private
40
40
 
41
41
  def success_message
42
- t('flash.actions.perform.notice', resource_name: @resource.class.model_name.human)
42
+ t('flash.actions.perform.notice', resource_name: @service.class.model_name.human)
43
43
  end
44
44
 
45
45
  def execute_service
@@ -58,15 +58,23 @@ module ServiceController::RestActions
58
58
  end
59
59
  end
60
60
 
61
- def initialize_service_for_invoke
62
- service_class.new
61
+ def initialize_service
62
+ @service = service_class.new({}, service_options)
63
+ end
64
+
65
+ def initialize_service_for_create
66
+ @service = service_class.new(hashified_params, service_options)
67
+ end
68
+
69
+ def service_options
70
+ default_options
63
71
  end
64
72
 
65
- def initialize_service_for_call
66
- service_class.new(hashified_params)
73
+ def default_options
74
+ { autosave: true }
67
75
  end
68
76
 
69
77
  def permitted_params
70
- raise "Not implemented"
78
+ raise "You have to implement permitted_params in #{self.class.name}."
71
79
  end
72
80
  end
@@ -1,22 +1,17 @@
1
1
  module ServiceController::RestServiceUrls
2
2
  extend ActiveSupport::Concern
3
-
4
- included do
5
- helper_method :collection_path,
6
- :call_service_path,
7
- :invoke_service_path
8
- end
9
3
 
10
- def collection_path
11
- resource_router.send(:url_for, { action: :index, only_path: true })
4
+ included do
5
+ helper_method :new_service_path,
6
+ :create_service_path
12
7
  end
13
8
 
14
- def invoke_service_path
15
- resource_router.send(:url_for, { action: :invoke, only_path: true })
9
+ def new_service_path
10
+ resource_router.send(:url_for, { action: :new, only_path: true })
16
11
  end
17
12
 
18
- def call_service_path
19
- resource_router.send(:url_for, { action: :call, only_path: true })
13
+ def create_service_path
14
+ resource_router.send(:url_for, { action: :create, only_path: true })
20
15
  end
21
16
 
22
17
  def resource_router
@@ -2,147 +2,10 @@ module ServiceController
2
2
  class Base < ::FrontendController
3
3
  layout 'rails/add_ons/application'
4
4
 
5
- module RestActions
6
- extend ActiveSupport::Concern
7
-
8
- included do
9
- include ActionController::MimeResponds
10
-
11
- respond_to :html
12
- responders :flash
13
-
14
- if respond_to?(:before_action)
15
- before_action :initialize_service, only: [:new]
16
- before_action :initialize_service_for_create, only: [:create]
17
- else
18
- before_filter :initialize_service, only: [:new]
19
- before_filter :initialize_service_for_create, only: [:create]
20
- end
21
- end
22
-
23
-
24
- def new; end
25
-
26
- def create
27
- perform
28
- end
29
-
30
- private
31
-
32
- def perform
33
- @result = @resource.perform
34
- if @result.success?
35
- flash.now[:success] = t('flash.actions.perform.notice', resource_name: @resource.class.model_name.human)
36
- render :create
37
- else
38
- render :new
39
- end
40
- end
41
-
42
- def initialize_service
43
- @resource = service_class.new
44
- end
45
-
46
- def initialize_service_for_create
47
- @resource = service_class.new(hashified_params)
48
- end
49
-
50
- def hashified_params
51
- if permitted_params.respond_to?(:to_h)
52
- permitted_params.to_h
53
- else
54
- permitted_params
55
- end
56
- end
57
-
58
- def permitted_params
59
- raise "not implemented"
60
- end
61
- end
62
-
63
- module Service
64
- extend ActiveSupport::Concern
65
-
66
- included do
67
- helper_method :service_class
68
- end
69
-
70
- def service_class
71
- self.class.service_class
72
- end
73
- end
74
-
75
- module RestResourceUrls
76
- extend ActiveSupport::Concern
77
-
78
- included do
79
- helper_method :new_resource_path
80
- helper_method :create_resource_path
81
- end
82
-
83
- private
84
-
85
- def new_resource_path
86
- url_for(action: :new, only_path: true)
87
- end
88
-
89
- def create_resource_path
90
- url_for(action: :create, only_path: true)
91
- end
92
- end
93
-
94
- module ResourceInflections
95
- extend ActiveSupport::Concern
96
-
97
- included do
98
- helper_method :inflections
99
- end
100
-
101
- private
102
-
103
- def inflections
104
- {
105
- service_name: service_class.model_name.human
106
- }
107
- end
108
- end
109
-
110
- module LocationHistory
111
- extend ActiveSupport::Concern
112
-
113
- included do
114
- if respond_to?(:before_action)
115
- before_action :store_location
116
- else
117
- before_filter :store_location
118
- end
119
- end
120
-
121
- private
122
-
123
- def store_location
124
- truncate_location_history(9)
125
- location_history[Time.zone.now] = request.referer
126
- end
127
-
128
- def location_history
129
- session[:location_history] ||= {}
130
- end
131
-
132
- def last_location
133
- location_history.sort.last.try(:last)
134
- end
135
-
136
- def truncate_location_history(count = 0)
137
- return if location_history.size <= count
138
- session[:location_history] = session[:location_history].sort.last(count).to_h
139
- end
140
- end
141
-
142
- include Service
143
- include RestActions
144
- include RestResourceUrls
145
- include ResourceInflections
146
- include LocationHistory
5
+ include ServiceController::Service
6
+ include ServiceController::RestActions
7
+ include ServiceController::RestServiceUrls
8
+ include ServiceController::ServiceInflections
9
+ include ServiceController::LocationHistory
147
10
  end
148
11
  end
@@ -25,5 +25,8 @@
25
25
  - if options[:block].present?
26
26
  %td{ td_options }= options[:block].call(resource)
27
27
  - else
28
- %td{ td_options }= resource.send(name)
29
-
28
+ %td{ td_options }
29
+ - if options[:label_method].present?
30
+ = resource.send(name).send(options[:label_method])
31
+ - else
32
+ = resource.send(name)
@@ -12,4 +12,7 @@
12
12
  = options[:block].call(resource)
13
13
  - else
14
14
  %td
15
- = resource.send(name)
15
+ - if options[:label_method].present?
16
+ = resource.send(name).send(options[:label_method])
17
+ - else
18
+ = resource.send(name)
@@ -1,6 +1,6 @@
1
- - if resource.errors.any?
1
+ - if service.errors.any?
2
2
  = bootstrap_alert(context: :danger, class: 'error-explanation') do
3
- .error-heading= t('errors.template.header', count: resource.errors.count, model: resource.class.model_name.human)
3
+ .error-heading= t('errors.template.header', count: service.errors.count, model: service.class.model_name.human)
4
4
  %ul
5
- - resource.errors.full_messages.each do |msg|
6
- %li= msg
5
+ - service.errors.full_messages.each do |msg|
6
+ %li= msg
@@ -9,6 +9,6 @@
9
9
 
10
10
  = render 'create_after_service_output', result: @result
11
11
 
12
- = bootstrap_button(to: new_resource_path, context: :success) do
12
+ = bootstrap_button(to: new_service_path, context: :success) do
13
13
  = fa_icon :back
14
14
  = t('.back')
@@ -1,6 +1,6 @@
1
1
  %h1= t('.title', inflections)
2
2
 
3
- = bootstrap_form_for(@resource, url: create_resource_path) do |f|
4
- = render 'form_errors', resource: f.object
3
+ = bootstrap_form_for(@service, url: create_service_path) do |f|
4
+ = render 'form_errors', service: f.object
5
5
  = render 'form', form: f
6
- = render 'form_buttons', form: f
6
+ = render 'form_buttons', form: f
@@ -1,5 +1,5 @@
1
1
  module Rails
2
2
  module AddOns
3
- VERSION = '1.5.2'.freeze
3
+ VERSION = '2.0.0'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-add_ons
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.2
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roberto Vasquez Angel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-07 00:00:00.000000000 Z
11
+ date: 2018-02-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -200,6 +200,7 @@ files:
200
200
  - app/concerns/resources_controller/rest_resource_urls.rb
201
201
  - app/concerns/resources_controller/sorting.rb
202
202
  - app/concerns/resources_controller/will_paginate.rb
203
+ - app/concerns/service_controller/location_history.rb
203
204
  - app/concerns/service_controller/rest_actions.rb
204
205
  - app/concerns/service_controller/rest_service_urls.rb
205
206
  - app/concerns/service_controller/service.rb