rails_admin_aasm 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d24976463c0ceaed6ca795a070b618a610f53f26
4
+ data.tar.gz: da372859af26075a7ba8f358fec4861c5f70431f
5
+ SHA512:
6
+ metadata.gz: 31baee41d0feeba6574e7f88ac25991a7896bcefc3fd9b938daaf6b46f0b060246c57ec4d04903bafa1aba829ab1da7327cda9f2896d55981293474f6206b91d
7
+ data.tar.gz: 426cf57cffe81523dbedac8139ca2ba63cc3b0cf45e163ef10aad530168b7780c1d4250f032ff440a99431e1104fdb9c41ffa98bcd8635689fae4550b23885be
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rails_admin_state_machine.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 glebtv
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # Rails Admin State Machine
2
+ ### Properly manage AASM states from rails_admin
3
+
4
+ Allows easily sending state_machine events to a model from Rails Admin, including support for
5
+ ActiveRecord \ Mongoid and custom state field name and multiple state machines per model.
6
+
7
+ Based on https://github.com/rs-pro/rails_admin_state.
8
+ Since state_machine currently is not maintained, I turn to AASM https://github.com/aasm/aasm.
9
+
10
+ ## Installing
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'rails_admin_aasm'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install rails_admin_aasm
23
+
24
+ ## Usage
25
+
26
+ Add the state action:
27
+
28
+ RailsAdmin.config do |config|
29
+ config.actions do
30
+ ......
31
+ state
32
+ end
33
+ end
34
+
35
+ Mark the field you need display as state:
36
+
37
+ rails_admin do
38
+ list do
39
+ field :state, :state
40
+ ...
41
+ end
42
+ edit do
43
+ field :state, :state
44
+ ...
45
+ end
46
+ ...
47
+ end
48
+
49
+ ### States and event button/label custom classes:
50
+
51
+ rails_admin do
52
+ list do
53
+ field :state, :state
54
+ ...
55
+ end
56
+ ...
57
+ state({
58
+ events: {dead: 'btn-danger', drain: 'btn-warning', alive: 'btn-success'},
59
+ states: {dead: 'label-important', drain: 'label-warning', alive: 'label-success'}
60
+ disable: [:dead] # disable some events so they are not shown.
61
+ })
62
+
63
+ end
64
+
65
+ Some classes are preset by default (published, deleted, etc)
66
+
67
+ ### CanCan integration
68
+
69
+ cannot :manage, Recipes::Recipe
70
+ can :read, Recipes::Recipe
71
+ can :state, Recipes::Recipe # required to do any transitions
72
+ can :all_events, Recipes::Recipe
73
+
74
+ ### i18n (state and event names):
75
+
76
+
77
+ ## Contributing
78
+
79
+ 1. Fork it
80
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
81
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
82
+ 4. Push to the branch (`git push origin my-new-feature`)
83
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,14 @@
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
+ $t.siblings().removeClass('active')
12
+ $t.addClass('active')
13
+ })
14
+ })
@@ -0,0 +1,10 @@
1
+ en:
2
+ admin:
3
+ actions:
4
+ toggle:
5
+ title: "Toggle"
6
+ state_machine:
7
+ event_fired: '%{attr} event %{event} fired'
8
+ error: "Error: %{err}"
9
+ no_id: No ID provided
10
+ no_event: Don't change
@@ -0,0 +1,11 @@
1
+ ru:
2
+ admin:
3
+ actions:
4
+ toggle:
5
+ title: "Переключить"
6
+ state_machine:
7
+ event_fired: '%{attr} выполнено событие %{event}'
8
+ error: "Ошибка: %{err}"
9
+ no_id: "Не указан ID"
10
+ no_event: "Не менять"
11
+
@@ -0,0 +1,55 @@
1
+ module RailsAdmin
2
+ module Config
3
+ module Actions
4
+ class State < Base
5
+ RailsAdmin::Config::Actions.register(self)
6
+
7
+ # Is the action acting on the root level (Example: /admin/contact)
8
+ register_instance_option :root? do
9
+ false
10
+ end
11
+
12
+ register_instance_option :collection? do
13
+ false
14
+ end
15
+
16
+ # Is the action on an object scope (Example: /admin/team/1/edit)
17
+ register_instance_option :member? do
18
+ true
19
+ end
20
+
21
+ register_instance_option :controller do
22
+ Proc.new do |klass|
23
+ unless @authorization_adapter.nil? || @authorization_adapter.authorized?(:all_events, @abstract_model, @object)
24
+ @authorization_adapter.try(:authorize, params[:event].to_sym, @abstract_model, @object)
25
+ end
26
+
27
+ @state_machine_options = ::RailsAdminAasm::Configuration.new @abstract_model
28
+ if params['id'].present?
29
+ begin
30
+ raise 'event disabled' if @state_machine_options.disabled?(params[:event].to_sym)
31
+ # if @object.send("fire_#{params[:attr]}_event".to_sym, params[:event].to_sym)
32
+ if @object.send("may_#{params[:event]}?".to_sym)
33
+ @object.send("#{params[:event]}!")
34
+ flash[:success] = I18n.t('admin.state_machine.event_fired', attr: params[:method], event: params[:event])
35
+ else
36
+ flash[:error] = obj.errors.full_messages.join(', ')
37
+ end
38
+ rescue Exception => e
39
+ Rails.logger.error e
40
+ flash[:error] = I18n.t('admin.state_machine.error', err: e.to_s)
41
+ end
42
+ else
43
+ flash[:error] = I18n.t('admin.state_machine.no_id')
44
+ end
45
+ redirect_to :back
46
+ end
47
+ end
48
+
49
+ register_instance_option :http_methods do
50
+ [:post]
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,65 @@
1
+ module RailsAdminAasm
2
+ class Configuration
3
+ def initialize(abstract_model)
4
+ @abstract_model = abstract_model
5
+ end
6
+
7
+ def options
8
+ @options ||= {
9
+ states: {
10
+ published: 'label-success',
11
+ sent: 'label-success',
12
+ done: 'label-success',
13
+ cancelled: 'label-important',
14
+ deleted: 'label-important',
15
+ trashed: 'label-important',
16
+ draft: 'label-important',
17
+ },
18
+ events: {
19
+ publish: 'btn-success',
20
+ confirm: 'btn-success',
21
+ send: 'btn-success',
22
+ done: 'btn-success',
23
+ cancel: 'btn-danger',
24
+ delete: 'btn-danger',
25
+ trash: 'btn-danger',
26
+ },
27
+ disable: []
28
+ }.merge(config)
29
+ @options
30
+ end
31
+
32
+ def state(name)
33
+ return '' if name.nil?
34
+ options[:states][name.to_sym] || ''
35
+ end
36
+
37
+ def event(name)
38
+ return '' if name.nil?
39
+ options[:events][name.to_sym] || ''
40
+ end
41
+
42
+ def disabled?(name)
43
+ return '' if name.nil?
44
+ options[:disable].include? name.to_sym
45
+ end
46
+
47
+ def authorize?
48
+ options[:authorize]
49
+ end
50
+
51
+ protected
52
+ def config
53
+ begin
54
+ opt = ::RailsAdmin::Config.model(@abstract_model.model).state
55
+ if opt.nil?
56
+ {}
57
+ else
58
+ opt
59
+ end
60
+ rescue
61
+ {}
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,4 @@
1
+ module RailsAdminAasm
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,100 @@
1
+ require 'builder'
2
+
3
+ module RailsAdmin
4
+ module Config
5
+ module Fields
6
+ module Types
7
+ class State < RailsAdmin::Config::Fields::Base
8
+ # Register field type for the type loader
9
+ RailsAdmin::Config::Fields::Types::register(self)
10
+ include RailsAdmin::Engine.routes.url_helpers
11
+
12
+ register_instance_option :pretty_value do
13
+ @state_machine_options = ::RailsAdminAasm::Configuration.new @abstract_model
14
+ v = bindings[:view]
15
+
16
+ state = bindings[:object].send(name)
17
+ state_class = @state_machine_options.state(state)
18
+ ret = [
19
+ '<div class="label ' + state_class + '">' + bindings[:object].aasm.human_state + '</div>',
20
+ '<div style="height: 10px;"></div>'
21
+ ]
22
+
23
+ bindings[:object].aasm.events.each do |event|
24
+ next unless v.authorized?(:state, @abstract_model, bindings[:object]) && (v.authorized?(:all_events, @abstract_model, bindings[:object]) || v.authorized?(event.name, @abstract_model, bindings[:object]))
25
+ event_class = @state_machine_options.event(event.name)
26
+ ret << bindings[:view].link_to(
27
+ event.name.to_s.capitalize,
28
+ state_path(model_name: @abstract_model, id: bindings[:object].id, event: event.name, attr: name),
29
+ method: :post,
30
+ class: "btn btn-mini #{event_class}",
31
+ style: 'margin-bottom: 5px;'
32
+ )
33
+ end
34
+ ('<div style="white-space: normal;">' + ret.join(' ') + '</div>').html_safe
35
+ end
36
+
37
+ register_instance_option :formatted_value do
38
+ form_value
39
+ end
40
+
41
+ register_instance_option :form_value do
42
+ @state_machine_options = ::RailsAdminAasm::Configuration.new @abstract_model
43
+ c = bindings[:controller]
44
+ v = bindings[:view]
45
+
46
+ state = bindings[:object].send(name)
47
+ state_class = @state_machine_options.state(state)
48
+ ret = [
49
+ '<div class="label ' + state_class + '">' + bindings[:object].aasm.human_state + '</div>',
50
+ '<div style="height: 10px;"></div>'
51
+ ]
52
+
53
+ empty = true
54
+ bindings[:object].aasm.events.each do |event|
55
+ next unless v.authorized?(:state, @abstract_model, bindings[:object]) && (v.authorized?(:all_events, @abstract_model, bindings[:object]) || v.authorized?(event.name, @abstract_model, bindings[:object]))
56
+ empty = false
57
+ event_class = @state_machine_options.event(event.name)
58
+ ret << bindings[:view].link_to(
59
+ event.name.to_s.capitalize,
60
+ '#',
61
+ 'data-attr' => name,
62
+ 'data-event' => event.name,
63
+ class: "state-btn btn btn-mini #{event_class}",
64
+ style: 'margin-bottom: 5px;'
65
+ )
66
+ end
67
+ unless empty
68
+ ret << bindings[:view].link_to(
69
+ I18n.t('admin.state_machine.no_event'),
70
+ '#',
71
+ 'data-attr' => name,
72
+ 'data-event' => '',
73
+ class: "state-btn btn btn-mini active",
74
+ style: 'margin-bottom: 5px;'
75
+ )
76
+ end
77
+ ('<div style="white-space: normal;">' + ret.join(' ') + '</div>').html_safe
78
+ end
79
+
80
+ register_instance_option :export_value do
81
+ state = bindings[:object].send(name)
82
+ bindings[:object].aasm.human_state
83
+ end
84
+
85
+ register_instance_option :partial do
86
+ :form_state
87
+ end
88
+
89
+ register_instance_option :allowed_methods do
90
+ [method_name, (method_name.to_s + '_event').to_sym]
91
+ end
92
+
93
+ register_instance_option :multiple? do
94
+ false
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,9 @@
1
+ module RailsAdmin
2
+ module Config
3
+ class Model
4
+ register_instance_option :state do
5
+ nil
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module RailsAdminAasm
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,10 @@
1
+ require "rails_admin_aasm/version"
2
+
3
+ require 'rails_admin/config/actions'
4
+ require 'rails_admin/config/model'
5
+
6
+ require 'rails_admin_aasm/configuration'
7
+ require 'rails_admin_aasm/action'
8
+ require 'rails_admin_aasm/model'
9
+ require 'rails_admin_aasm/field'
10
+ require 'rails_admin_aasm/engine'
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rails_admin_aasm/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails_admin_aasm"
8
+ spec.version = RailsAdminAasm::VERSION
9
+ spec.authors = ["Chuanpin Zhu"]
10
+ spec.email = ["zcppku@gmail.com"]
11
+ spec.description = %q{Manage model's state with AASM and rails_admin}
12
+ spec.summary = %q{Integrate aasm with rails_admin, add a new type into rails_admin, which is state}
13
+ spec.homepage = "https://github.com/zcpdog/rails_admin_aasm"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "rails_admin", "~> 0"
22
+ spec.add_dependency "aasm", "~> 0"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake", "~> 0"
26
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_admin_aasm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chuanpin Zhu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails_admin
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: aasm
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Manage model's state with AASM and rails_admin
70
+ email:
71
+ - zcppku@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - app/views/rails_admin/main/_form_state.html.haml
82
+ - config/locales/en.rails_admin_aasm.yml
83
+ - config/locales/ru.rails_admin_aasm.yml
84
+ - lib/rails_admin_aasm.rb
85
+ - lib/rails_admin_aasm/action.rb
86
+ - lib/rails_admin_aasm/configuration.rb
87
+ - lib/rails_admin_aasm/engine.rb
88
+ - lib/rails_admin_aasm/field.rb
89
+ - lib/rails_admin_aasm/model.rb
90
+ - lib/rails_admin_aasm/version.rb
91
+ - rails_admin_aasm.gemspec
92
+ homepage: https://github.com/zcpdog/rails_admin_aasm
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.2.2
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Integrate aasm with rails_admin, add a new type into rails_admin, which is
116
+ state
117
+ test_files: []