effective_resources 2.4.7 → 2.5.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
  SHA256:
3
- metadata.gz: 59a393d33cfa210550260fe050c11b1eab54ff0b7f534391aad7bb2058a01ea0
4
- data.tar.gz: 5edbfb9aa5538100895dc35e2fb4806a5915c730862d9e312f4900e858914b66
3
+ metadata.gz: 856d2a15f81a4ecff7c02f01a0e6444e5d81999af6f357428b86a6479bd126fb
4
+ data.tar.gz: 72a6da8d12e0b5ce4465028fe5269ca2ef22cfb3f4f70655783cf8db1ecd97b5
5
5
  SHA512:
6
- metadata.gz: bb45c37f2c5e2afef660925a218aceb7ee93f6ed7cc3e670c82e002edce9c9670e5a93604c677ea792999ecfa14ec93dfc25050de9ff0a66a151cf8ac9ebd568
7
- data.tar.gz: cd3e71440f3693dcb12278dea707273c974beff4dbd6801546d199d618dc731877f4487726a8c4519f4beb1d3492c727f693dc5d5b9a706dfee6dc2e214d6b88
6
+ metadata.gz: 3a18cacf16dd2d0c54f14b4995f73708b9136baf1701d75d3d0cc5c186b782bee8bee3fc49a54658da1794b9faac59dc6544f8a53a9eddbe033c25af6360e0bd
7
+ data.tar.gz: fea55cc3cc15071d6bf80a34b4bbb1ab975d5f173434f4589277a9ed1885166f205b0c2c710c1c73ffc169c5a624daa6f1ada46f16aa0941e4b3cb6a145d7634
@@ -3,6 +3,7 @@
3
3
  module EffectiveResourcesPrivateHelper
4
4
  REPLACE_PAGE_ACTIONS = {'update' => :edit, 'create' => :new}
5
5
  BLACKLIST = [:default, :only, :except, :if, :unless, :redirect, :success, :danger, :klass]
6
+ REPLACE_RESOURCE_METHODS = Regexp.new('@resource\.\w+')
6
7
 
7
8
  def permitted_resource_actions(resource, actions)
8
9
  page_action = REPLACE_PAGE_ACTIONS[params[:action]] || params[:action].try(:to_sym) || :save
@@ -20,7 +21,7 @@ module EffectiveResourcesPrivateHelper
20
21
  next unless permitted
21
22
 
22
23
  opts = args.except(:default, :only, :except, :if, :unless, :redirect, :success, :danger, :klass)
23
- resource_to_s = resource.to_s.presence || resource.class.name.underscore
24
+ resource_to_s = resource.to_s.presence || EffectiveResources.et(resource)
24
25
 
25
26
  # Transform data: { ... } hash into 'data-' keys
26
27
  if opts.key?(:data)
@@ -35,9 +36,16 @@ module EffectiveResourcesPrivateHelper
35
36
  opts[:href] = opts.delete(:url)
36
37
  end
37
38
 
38
- # Replace resource name in any token strings
39
+ # Replaces @resource.emails_send_to and @resource in data-confirm
39
40
  if opts.key?('data-confirm') && opts['data-confirm'].to_s.include?('@resource')
40
- opts['data-confirm'] = opts['data-confirm'].gsub('@resource', resource_to_s)
41
+ confirm = opts['data-confirm']
42
+
43
+ confirm.gsub!(REPLACE_RESOURCE_METHODS) do |value|
44
+ method = value.to_s.split('.').last
45
+ resource.public_send(method)
46
+ end
47
+
48
+ opts['data-confirm'] = confirm.gsub('@resource', resource_to_s)
41
49
  end
42
50
 
43
51
  # Assign class
@@ -56,10 +64,10 @@ module EffectiveResourcesPrivateHelper
56
64
  # Assign title
57
65
  opts[:title] ||= case opts[:action]
58
66
  when :save then commit
59
- when :edit then "Edit #{resource_to_s}"
67
+ when :edit then "#{et("effective_resources.actions.edit")} #{resource_to_s}"
60
68
  when :show then "#{resource_to_s}"
61
- when :destroy then "Delete #{resource_to_s}"
62
- when :index then "All #{resource.class.name.gsub('::', ' ').underscore.gsub('_', ' ').titleize.pluralize}"
69
+ when :destroy then "#{et("effective_resources.actions.destroy")} #{resource_to_s}"
70
+ when :index then "#{et("effective_resources.actions.index")} #{ets(resource)}"
63
71
  else "#{opts[:action].to_s.titleize} #{resource_to_s}"
64
72
  end
65
73
 
@@ -25,44 +25,45 @@ module Effective
25
25
  end
26
26
 
27
27
  def buttons
28
+ human_index = EffectiveResources.et("effective_resources.actions.index")
29
+ human_new = EffectiveResources.et("effective_resources.actions.new")
30
+
28
31
  {}.tap do |buttons|
29
32
  member_get_actions.each do |action| # default true means it will be overwritten by dsl methods
30
- buttons[action.to_s.titleize] = { action: action, default: true }
33
+ buttons[human_action_name(action)] = { action: action, default: true }
31
34
  end
32
35
 
33
36
  (member_post_actions - crud_actions).each do |action| # default true means it will be overwritten by dsl methods
34
- action_name = action.to_s.titleize
37
+ name = human_action_name(action)
38
+ confirm = human_action_confirm(action)
35
39
 
36
- buttons[action_name] = case action
40
+ buttons[name] = case action
37
41
  when :archive
38
- { action: action, default: true, if: -> { !resource.archived? }, class: 'btn btn-danger', 'data-method' => :post, 'data-confirm' => "Really #{action_name} @resource?"}
42
+ { action: action, default: true, if: -> { !resource.archived? }, class: 'btn btn-danger', 'data-method' => :post, 'data-confirm' => confirm }
39
43
  when :unarchive
40
- { action: action, default: true, if: -> { resource.archived? }, 'data-method' => :post, 'data-confirm' => "Really #{action_name} @resource?" }
44
+ { action: action, default: true, if: -> { resource.archived? }, 'data-method' => :post, 'data-confirm' => confirm }
41
45
  else
42
- { action: action, default: true, 'data-method' => :post, 'data-confirm' => "Really #{action_name} @resource?"}
46
+ { action: action, default: true, 'data-method' => :post, 'data-confirm' => confirm }
43
47
  end
44
48
  end
45
49
 
46
50
  member_delete_actions.each do |action|
47
- action_name = action.to_s.titleize
51
+ name = human_action_name(action)
52
+ confirm = human_action_confirm(action)
48
53
 
49
- if action == :destroy
50
- buttons['Delete'] = { action: action, default: true, 'data-method' => :delete, 'data-confirm' => "Really delete @resource?" }
51
- else
52
- buttons[action_name] = { action: action, default: true, 'data-method' => :delete, 'data-confirm' => "Really #{action_name} @resource?" }
53
- end
54
+ buttons[name] = { action: action, default: true, 'data-method' => :delete, 'data-confirm' => confirm }
54
55
  end
55
56
 
56
57
  if collection_get_actions.find { |a| a == :index }
57
- buttons["All #{human_plural_name}"] = { action: :index, default: true }
58
+ buttons["#{human_index} #{human_plural_name}"] = { action: :index, default: true }
58
59
  end
59
60
 
60
61
  if collection_get_actions.find { |a| a == :new }
61
- buttons["New #{human_name}"] = { action: :new, default: true }
62
+ buttons["#{human_new} #{human_name}"] = { action: :new, default: true }
62
63
  end
63
64
 
64
65
  (collection_get_actions - crud_actions).each do |action|
65
- buttons[action.to_s.titleize] = { action: action, default: true }
66
+ buttons[human_action_name(action)] = { action: action, default: true }
66
67
  end
67
68
  end
68
69
  end
@@ -73,37 +74,35 @@ module Effective
73
74
  {}.tap do |actions|
74
75
  member_get_actions.reverse_each do |action|
75
76
  next unless crud_actions.include?(action)
76
- actions[action.to_s.titleize] = { action: action, default: true }
77
+ actions[human_action_name(action)] = { action: action, default: true }
77
78
  end
78
79
 
79
80
  member_get_actions.each do |action|
80
81
  next if crud_actions.include?(action)
81
- actions[action.to_s.titleize] = { action: action, default: true }
82
+ actions[human_action_name(action)] = { action: action, default: true }
82
83
  end
83
84
 
84
85
  member_post_actions.each do |action|
85
86
  next if crud_actions.include?(action)
86
87
 
87
- action_name = action.to_s.titleize
88
+ name = human_action_name(action)
89
+ confirm = human_action_confirm(action)
88
90
 
89
- actions[action_name] = case action
91
+ actions[name] = case action
90
92
  when :archive
91
- { action: action, default: true, if: -> { !resource.archived? }, class: 'btn btn-danger', 'data-method' => :post, 'data-confirm' => "Really #{action_name} @resource?"}
93
+ { action: action, default: true, if: -> { !resource.archived? }, class: 'btn btn-danger', 'data-method' => :post, 'data-confirm' => confirm }
92
94
  when :unarchive
93
- { action: action, default: true, if: -> { resource.archived? }, 'data-method' => :post, 'data-confirm' => "Really #{action_name} @resource?" }
95
+ { action: action, default: true, if: -> { resource.archived? }, 'data-method' => :post, 'data-confirm' => confirm }
94
96
  else
95
- { action: action, default: true, 'data-method' => :post, 'data-confirm' => "Really #{action_name} @resource?" }
97
+ { action: action, default: true, 'data-method' => :post, 'data-confirm' => confirm }
96
98
  end
97
99
  end
98
100
 
99
101
  member_delete_actions.each do |action|
100
- action_name = action.to_s.titleize
102
+ name = human_action_name(action)
103
+ confirm = human_action_confirm(action)
101
104
 
102
- if action == :destroy
103
- actions['Delete'] = { action: action, default: true, 'data-method' => :delete, 'data-confirm' => "Really delete @resource?" }
104
- else
105
- actions[action_name] = { action: action, default: true, 'data-method' => :delete, 'data-confirm' => "Really #{action_name} @resource?" }
106
- end
105
+ actions[name] = { action: action, default: true, 'data-method' => :delete, 'data-confirm' => confirm }
107
106
  end
108
107
  end
109
108
  end
@@ -111,26 +110,29 @@ module Effective
111
110
  # Used by datatables
112
111
  def fallback_resource_actions
113
112
  {
114
- 'Show': { action: :show, default: true },
115
- 'Edit': { action: :edit, default: true },
116
- 'Delete': { action: :destroy, default: true, 'data-method' => :delete, 'data-confirm' => "Really delete @resource?" }
113
+ human_action_name(:show) => { action: :show, default: true },
114
+ human_action_name(:edit) => { action: :edit, default: true },
115
+ human_action_name(:destroy) => { action: :destroy, default: true, 'data-method' => :delete, 'data-confirm' => human_action_confirm(:destroy) }
117
116
  }
118
117
  end
119
118
 
120
119
  # This is the fallback for render_resource_actions when no actions are specified, but a class is given
121
120
  # Used by Datatables new
122
121
  def resource_klass_actions
122
+ human_index = EffectiveResources.et("effective_resources.actions.index")
123
+ human_new = EffectiveResources.et("effective_resources.actions.new")
124
+
123
125
  {}.tap do |buttons|
124
126
  if collection_get_actions.find { |a| a == :index }
125
- buttons["All #{human_plural_name}"] = { action: :index, default: true }
127
+ buttons["#{human_index} #{human_plural_name}"] = { action: :index, default: true }
126
128
  end
127
129
 
128
130
  if collection_get_actions.find { |a| a == :new }
129
- buttons["New #{human_name}"] = { action: :new, default: true }
131
+ buttons["#{human_new} #{human_name}"] = { action: :new, default: true }
130
132
  end
131
133
 
132
134
  (collection_get_actions - crud_actions).each do |action|
133
- buttons[action.to_s.titleize] = { action: action, default: true }
135
+ buttons[human_action_name(action)] = { action: action, default: true }
134
136
  end
135
137
  end
136
138
  end
@@ -4,6 +4,29 @@ module Effective
4
4
  module Resources
5
5
  module I18n
6
6
 
7
+ def human_action_name(action)
8
+ if klass.respond_to?(:model_name)
9
+ value = ::I18n.t("activerecord.actions.#{klass.model_name.i18n_key}.#{action}")
10
+ return value unless value.start_with?('translation missing:')
11
+ end
12
+
13
+ if(crud_actions.include?(action))
14
+ # Raises exception if not present
15
+ return EffectiveResources.et("effective_resources.actions.#{action}")
16
+ end
17
+
18
+ action.to_s.titleize
19
+ end
20
+
21
+ def human_action_confirm(action)
22
+ if klass.respond_to?(:model_name)
23
+ value = ::I18n.t("activerecord.actions.#{klass.model_name.i18n_key}.#{action}_confirm")
24
+ return value unless value.start_with?('translation missing:')
25
+ end
26
+
27
+ "Really #{human_action_name(action)} @resource?"
28
+ end
29
+
7
30
  def human_name
8
31
  if klass.respond_to?(:model_name)
9
32
  klass.model_name.human
@@ -0,0 +1,10 @@
1
+ en:
2
+ effective_resources:
3
+ name: 'Effective Resources'
4
+
5
+ actions:
6
+ index: 'All'
7
+ new: 'New'
8
+ edit: 'Edit'
9
+ show: 'Show'
10
+ destroy: 'Delete'
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '2.4.7'.freeze
2
+ VERSION = '2.5.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.7
4
+ version: 2.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-20 00:00:00.000000000 Z
11
+ date: 2023-03-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -212,6 +212,7 @@ files:
212
212
  - app/views/effective/resource/_actions_glyphicons.html.haml
213
213
  - app/views/layouts/effective_mailer_layout.html.haml
214
214
  - config/effective_resources.rb
215
+ - config/locales/effective_resources.en.yml
215
216
  - config/routes.rb
216
217
  - lib/effective_resources.rb
217
218
  - lib/effective_resources/effective_gem.rb