effective_resources 0.7.3 → 0.7.4

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: 4dce484ce7125d0bf8cb2c928f983a34d37fcd10
4
- data.tar.gz: e30f9a165335f8426d4f27e426599b498eb819f4
3
+ metadata.gz: 93a12b8c4e3ed0c19944e519a9744e2147df4dfd
4
+ data.tar.gz: ab6a80d74be88e195c5b456ec60d02637de9ddad
5
5
  SHA512:
6
- metadata.gz: 2f2e2d4d18cdecc0587222de2a94cd5176cea93bf087ad19cf52eccd1db400d4bce66df706770b77a32f3b9f9e8070f5a166fa703f42a6456e751b2a7f155414
7
- data.tar.gz: 39127cb6986dcf152ab0de1642ca81afb7c58beead37e77cf3888064083376844193851e812fd42876cd9d1cd07b2675a7d9035f6d08801b36be5944e83f81e8
6
+ metadata.gz: 95a9c3623d279ac5901cab849f8c67f8e980f70eba46f48d0259eb3fa7e8a80120f5c37b293ed64b75662f95601e70a30c341118b7746577444b7d3a98260d8a
7
+ data.tar.gz: 9b9a58a7686fab92936570e9add5116fcb45357ebfe829cbe0b306c5209aa64cff2c9e8e1f435a89dd8eccbdae1f2ec03da7412eeb2ede69aaa931f03051bd69
@@ -5,7 +5,11 @@ module Effective
5
5
  included do
6
6
  class << self
7
7
  def member_actions
8
- @_effective_member_actions ||= {
8
+ @_effective_member_actions ||= {}
9
+ end
10
+
11
+ def _default_member_actions
12
+ {
9
13
  'Save' => { action: :save, data: { disable_with: 'Saving...' }},
10
14
  'Continue' => { action: :save, data: { disable_with: 'Saving...' }},
11
15
  'Add New' => { action: :save, data: { disable_with: 'Saving...' }}
@@ -95,6 +99,11 @@ module Effective
95
99
  end
96
100
  end
97
101
 
102
+ # Applies the default actions
103
+ def default_actions(args = {})
104
+ default_member_actions.each { |k, v| member_actions[k] = (args || {}).merge(v) }
105
+ end
106
+
98
107
  # page_title 'My Title', only: [:new]
99
108
  def page_title(label = nil, opts = {}, &block)
100
109
  raise 'expected a label or block' unless (label || block_given?)
@@ -279,21 +288,32 @@ module Effective
279
288
  render json: { status: 200, message: "Successfully #{action_verb(action)} #{successes} / #{resources.length} selected #{resource_plural_name}" }
280
289
  end
281
290
 
291
+
292
+
293
+ # The block must implement a comparison between a and b and return an integer
294
+ # less than 0 when b follows a, 0 when a and b are equivalent, or an integer greater than 0 when a follows b.
295
+
282
296
  # Here we look at all available (class level) member actions, see which ones apply to the current resource
283
297
  # This feeds into the helper simple_form_submit(f)
284
298
  # Returns a Hash of {'Save': {data-disable-with: 'Saving...'}, 'Approve': {data-disable-with: 'Approve'}}
285
299
  def member_actions_for(obj)
286
- self.class.member_actions.select do |commit, args|
300
+ actions = (self.class.member_actions.presence || self.class._default_member_actions)
301
+
302
+ actions.select do |commit, args|
303
+ args[:class] = args[:class].to_s
304
+
287
305
  (args.key?(:if) ? obj.instance_exec(&args[:if]) : true) &&
288
306
  (args.key?(:unless) ? !obj.instance_exec(&args[:unless]) : true)
289
307
  end.sort do |(commit_x, x), (commit_y, y)|
290
- danger = (x[:class].to_s.index('danger') || -1) <=> (y[:class].to_s.index('danger') || -1)
291
- danger = nil if danger == 0
308
+ # Sort to front
309
+ primary = (y[:class].include?('primary') ? 1 : 0) - (x[:class].include?('primary') ? 1 : 0)
310
+ primary = nil if primary == 0
292
311
 
293
- save_action = (x[:action] == :save ? 0 : -1) <=> (y[:action] == :save ? 0 : -1)
294
- save_action = nil if save_action == 0
312
+ # Sort to back
313
+ danger = (x[:class].include?('danger') ? 1 : 0) - (y[:class].include?('danger') ? 1 : 0)
314
+ danger = nil if danger == 0
295
315
 
296
- danger || save_action || 0
316
+ primary || danger || actions.keys.index(commit_x) <=> actions.keys.index(commit_y)
297
317
  end.inject({}) do |h, (commit, args)|
298
318
  h[commit] = args.except(:action, :if, :unless, :redirect); h
299
319
  end
@@ -422,7 +442,7 @@ module Effective
422
442
  end
423
443
 
424
444
  def commit_action
425
- self.class.member_actions[params[:commit].to_s] || self.class.member_actions['Save'] || raise("expected member_actions['Save'] to be present")
445
+ self.class.member_actions[params[:commit].to_s] || { action: :save }
426
446
  end
427
447
 
428
448
  # Returns an ActiveRecord relation based on the computed value of `resource_scope` dsl method
@@ -52,7 +52,7 @@ module EffectiveBootstrap3Helper
52
52
  end
53
53
 
54
54
  def tab(label, &block)
55
- controls = label.to_s.downcase.to_param
55
+ controls = label.to_s.parameterize.gsub('_', '-')
56
56
  active = (@_tab_active == :first || @_tab_active == label)
57
57
 
58
58
  @_tab_active = nil if @_tab_active == :first
@@ -58,6 +58,21 @@ module EffectiveResourcesHelper
58
58
  end
59
59
  end
60
60
 
61
+ # When called from /admin/things/new.html.haml this will render 'admin/things/form', or 'things/form', or 'thing/form'
62
+ def render_resource_form(resource)
63
+ atts = {:namespace => (resource.namespace.to_sym if resource.namespace.present?), resource.name.to_sym => instance_variable_get('@' + resource.name)}.compact
64
+
65
+ if lookup_context.template_exists?('form', controller._prefixes, :partial)
66
+ render 'form', atts
67
+ elsif lookup_context.template_exists?('form', resource.plural_name, :partial)
68
+ render "#{resource.plural_name}/form", atts
69
+ elsif lookup_context.template_exists?('form', resource.name, :partial)
70
+ render "#{resource.name}/form", atts
71
+ else
72
+ render 'form', atts # Will raise the regular error
73
+ end
74
+ end
75
+
61
76
  def number_to_duration(duration)
62
77
  duration = duration.to_i
63
78
  value = duration.abs
@@ -13,4 +13,4 @@
13
13
  - if EffectiveResources.authorized?(controller, :destroy, @resource) && (path = resource.action_path(:destroy, @resource)).present?
14
14
  = link_to 'Delete', path, class: 'btn btn-sm btn-danger', data: { confirm: "Really delete #{@resource}?", method: :delete }
15
15
 
16
- = render 'form', resource.name.to_sym => @resource
16
+ = render_resource_form(resource)
@@ -1,6 +1,9 @@
1
1
  - resource = (@_effective_resource || Effective::Resource.new(controller_path))
2
+ - @resource = instance_variable_get('@' + resource.name) if resource.name
2
3
 
3
- %h1= @page_title
4
+ - if @resource
5
+ .row
6
+ .col-xs-8
7
+ %h1= @page_title
4
8
 
5
- - if resource.name.present? && instance_variable_get('@' + resource.name)
6
- = render 'form', resource.name.to_sym => instance_variable_get('@' + resource.name)
9
+ = render_resource_form(resource)
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '0.7.3'.freeze
2
+ VERSION = '0.7.4'.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: 0.7.3
4
+ version: 0.7.4
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: 2017-12-11 00:00:00.000000000 Z
11
+ date: 2017-12-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails