class RailsAdmin::Config::Fields::Types::State

Public Class Methods

pretty_value_from_params(name, abstract_model, obj, authorization_adapter, view, state_machine_options, read_only = false) click to toggle source
# File lib/rails_admin_state/field.rb, line 105
          def self.pretty_value_from_params name, abstract_model, obj, authorization_adapter, view, state_machine_options, read_only = false
            state = obj.send(name)
            state_class = state_machine_options.state(state)
            s = obj.class.state_machines[name.to_sym].states[state.to_sym]
            ret = [
              '<div class="label ' + state_class + '">' + s.human_name + '</div>',
              '<div style="height: 10px;"></div>'
            ]
            unless read_only
              events = obj.class.state_machines[name.to_sym].events
              obj.send("#{name}_events".to_sym).each do |event|
                next if state_machine_options.disabled?(event)
                unless authorization_adapter.nil?
                  adapter = authorization_adapter
                  next unless (adapter.authorized?(:state, abstract_model, obj) && (adapter.authorized?(:all_events, abstract_model, obj) || adapter.authorized?(event, abstract_model, obj)))
                end
                event_class = state_machine_options.event(event)
                ret << ActionController::Base.helpers.link_to(
                  events[event].human_name,
                  view.state_path(model_name: abstract_model, id: obj.id, event: event, attr: name),
                  method: :post,
                  class: "btn btn-mini btn-xs #{event_class}",
                  style: 'margin-bottom: 5px;',
                  onclick: <<-END.strip_heredoc.gsub("\n", ' ').gsub(/ +/, ' ')
                    event.preventDefault();
                    event.stopPropagation();
                    var $t = $(this);
                    var old_html = $t.html();
                    $t.html("<i class='fa fa-spinner fa-spin'></i>");
                    $.ajax({
                      type: "POST",
                      url: $t.attr("href"),
                      data: {ajax:true},
                      success: function(r) {
                        $t.parent().parent().html(r.html);
                      },
                      error: function(e) {
                        $t.html(old_html);
                        if(e.responseJSON && e.responseJSON.error) {
                          alert(e.responseJSON.error);
                        } else {
                          alert(e.responseText);
                        }
                      }
                    });
                    return false;
                  END
                )
              end
            end
            ('<div style="white-space: normal;">' + ret.join(' ') + '</div>').html_safe
          end