rails_admin_state 1.2.0 → 1.3.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: 6d212ee119619ef85b445d0c25598d3a0e80b747
4
- data.tar.gz: 1b0e5d57c8db6ad968e19c914a29c32a5e8c8882
3
+ metadata.gz: 6e934a56df4a247bf6057a76e6950dba086438dc
4
+ data.tar.gz: 097c47d5b172b100053bfc6f779ddba75efdc79e
5
5
  SHA512:
6
- metadata.gz: 1a7cf527ca9611cba7896a5ec60e965d8049f6e8c192ab1bc0c45ae81330f3a91c5725c5886365323fe0241c034d084e917929a562bc01f5339cd7eb047a1c92
7
- data.tar.gz: c459ce5dd783bc94d858ccf699e31f0784ba23f4c890bf85c88822b680fd2630f75940ad88152a625d52e32d5919d52893736524749592aab3c842bd695674f6
6
+ metadata.gz: 2f0fa99675c1349e4119537dba9bf2e8d3f3d526bb3e53dd4034f319da899f60a50e6b3831ed1b92441a6cb72b5a155bbebeaa6885f342a553093bbc69729b8b
7
+ data.tar.gz: 013b0afe86097e5bf610bcb34ad1e026437584b04dfb485a239fbcb52071b8ad224448edaefd5f2bf7081fa04f38e952ce9f99a82ea4632b6d1e1a40c4d81b38
data/README.md CHANGED
@@ -66,6 +66,13 @@ Mark the field you need display as state:
66
66
 
67
67
  Some classes are preset by default (published, deleted, etc)
68
68
 
69
+ ### CanCan integration
70
+
71
+ cannot :manage, Recipes::Recipe
72
+ can :read, Recipes::Recipe
73
+ can :state, Recipes::Recipe # required to do any transitions
74
+ can :all_events, Recipes::Recipe
75
+
69
76
  ### i18n (state and event names):
70
77
 
71
78
  Just as usual for state_machine, see:
@@ -1 +1,16 @@
1
- = field.pretty_value
1
+ .state-widget
2
+ = form.hidden_field field.method_name.to_s + '_event'
3
+ = field.form_value
4
+ :javascript
5
+ $(function() {
6
+ $('.state-btn').off('click').click(function(e){
7
+ e.preventDefault();
8
+ var $t = $(this), d = $t.data();
9
+ var $i = $t.parents('.state-widget').find('input')
10
+ $i.val(d.event);
11
+ console.log($i)
12
+ $t.siblings().removeClass('active')
13
+ $t.addClass('active')
14
+ })
15
+ })
16
+
@@ -7,3 +7,4 @@ en:
7
7
  event_fired: '%{attr} event %{event} fired'
8
8
  error: "Error: %{err}"
9
9
  no_id: No ID provided
10
+ no_event: Don't change
@@ -7,4 +7,5 @@ ru:
7
7
  event_fired: '%{attr} выполнено событие %{event}'
8
8
  error: "Ошибка: %{err}"
9
9
  no_id: "Не указан ID"
10
+ no_event: "Не менять"
10
11
 
@@ -20,18 +20,22 @@ module RailsAdmin
20
20
 
21
21
  register_instance_option :controller do
22
22
  Proc.new do |klass|
23
+ unless @authorization_adapter.authorized?(:all_events, @abstract_model, @object)
24
+ @authorization_adapter.try(:authorize, params[:event].to_sym, @abstract_model, @object)
25
+ end
26
+
23
27
  @state_machine_options = ::RailsAdminState::Configuration.new @abstract_model
24
28
  if params['id'].present?
25
29
  begin
26
30
  raise 'event disabled' if @state_machine_options.disabled?(params[:event].to_sym)
27
- obj = @abstract_model.model.find(params['id'])
28
- if obj.send("fire_#{params[:attr]}_event".to_sym, params[:event].to_sym)
29
- obj.save!
31
+ if @object.send("fire_#{params[:attr]}_event".to_sym, params[:event].to_sym)
32
+ @object.save!
30
33
  flash[:success] = I18n.t('admin.state_machine.event_fired', attr: params[:method], event: params[:event])
31
34
  else
32
35
  flash[:error] = obj.errors.full_messages.join(', ')
33
36
  end
34
37
  rescue Exception => e
38
+ Rails.logger.error e
35
39
  flash[:error] = I18n.t('admin.state_machine.error', err: e.to_s)
36
40
  end
37
41
  else
@@ -44,6 +44,9 @@ module RailsAdminState
44
44
  options[:disable].include? name.to_sym
45
45
  end
46
46
 
47
+ def authorize?
48
+ options[:authorize]
49
+ end
47
50
 
48
51
  protected
49
52
  def config
@@ -11,6 +11,7 @@ module RailsAdmin
11
11
 
12
12
  register_instance_option :pretty_value do
13
13
  @state_machine_options = ::RailsAdminState::Configuration.new @abstract_model
14
+ v = bindings[:view]
14
15
 
15
16
  state = bindings[:object].send(name)
16
17
  state_class = @state_machine_options.state(state)
@@ -23,6 +24,7 @@ module RailsAdmin
23
24
  events = bindings[:object].class.state_machines[name.to_sym].events
24
25
  bindings[:object].send("#{name}_events".to_sym).each do |event|
25
26
  next if @state_machine_options.disabled?(event)
27
+ next unless v.authorized?(:state, @abstract_model, bindings[:object]) && (v.authorized?(:all_events, @abstract_model, bindings[:object]) || v.authorized?(event, @abstract_model, bindings[:object]))
26
28
  event_class = @state_machine_options.event(event)
27
29
  ret << bindings[:view].link_to(
28
30
  events[event].human_name,
@@ -36,13 +38,59 @@ module RailsAdmin
36
38
  end
37
39
 
38
40
  register_instance_option :formatted_value do
39
- pretty_value
41
+ form_value
42
+ end
43
+
44
+ register_instance_option :form_value do
45
+ @state_machine_options = ::RailsAdminState::Configuration.new @abstract_model
46
+ c = bindings[:controller]
47
+ v = bindings[:view]
48
+
49
+ state = bindings[:object].send(name)
50
+ state_class = @state_machine_options.state(state)
51
+ s = bindings[:object].class.state_machines[name.to_sym].states[state.to_sym]
52
+ ret = [
53
+ '<div class="label ' + state_class + '">' + s.human_name + '</div>',
54
+ '<div style="height: 10px;"></div>'
55
+ ]
56
+
57
+ events = bindings[:object].class.state_machines[name.to_sym].events
58
+ empty = true
59
+ bindings[:object].send("#{name}_events".to_sym).each do |event|
60
+ next if @state_machine_options.disabled?(event)
61
+ next unless v.authorized?(:state, @abstract_model, bindings[:object]) && (v.authorized?(:all_events, @abstract_model, bindings[:object]) || v.authorized?(event, @abstract_model, bindings[:object]))
62
+ empty = false
63
+ event_class = @state_machine_options.event(event)
64
+ ret << bindings[:view].link_to(
65
+ events[event].human_name,
66
+ '#',
67
+ 'data-attr' => name,
68
+ 'data-event' => event,
69
+ class: "state-btn btn btn-mini #{event_class}",
70
+ style: 'margin-bottom: 5px;'
71
+ )
72
+ end
73
+ unless empty
74
+ ret << bindings[:view].link_to(
75
+ I18n.t('admin.state_machine.no_event'),
76
+ '#',
77
+ 'data-attr' => name,
78
+ 'data-event' => '',
79
+ class: "state-btn btn btn-mini active",
80
+ style: 'margin-bottom: 5px;'
81
+ )
82
+ end
83
+ ('<div style="white-space: normal;">' + ret.join(' ') + '</div>').html_safe
40
84
  end
41
85
 
42
86
  register_instance_option :partial do
43
87
  :form_state
44
88
  end
45
89
 
90
+ register_instance_option :allowed_methods do
91
+ [method_name, (method_name.to_s + '_event').to_sym]
92
+ end
93
+
46
94
  register_instance_option :multiple? do
47
95
  false
48
96
  end
@@ -1,3 +1,3 @@
1
1
  module RailsAdminState
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_admin_state
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - glebtv