Class: Avo::Actions::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/avo/app/action.rb

Constant Summary collapse

@@default =
nil
@@fields =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAction

Returns a new instance of Action.



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/avo/app/action.rb', line 38

def initialize
  @name ||= name
  @message ||= 'Are you sure you want to run this action?'
  @default ||= ''
  @fields ||= []
  @confirm_text = 'Run'
  @cancel_text = 'Cancel'
  @response ||= {}
  @response[:message_type] ||= :success
  @response[:message] ||= 'Action ran successfully!'
  @theme ||= 'success'
end

Instance Attribute Details

#cancel_textObject

Returns the value of attribute cancel_text.



9
10
11
# File 'lib/avo/app/action.rb', line 9

def cancel_text
  @cancel_text
end

#confirm_textObject

Returns the value of attribute confirm_text.



8
9
10
# File 'lib/avo/app/action.rb', line 8

def confirm_text
  @confirm_text
end

#defaultObject

Returns the value of attribute default.



6
7
8
# File 'lib/avo/app/action.rb', line 6

def default
  @default
end

#messageObject

Returns the value of attribute message.



5
6
7
# File 'lib/avo/app/action.rb', line 5

def message
  @message
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/avo/app/action.rb', line 4

def name
  @name
end

#responseObject

response:

message: String
message_type: success | error
type: reload | reload_resources | redirect | http_redirect | open_in_new_tab | download
path: String
filename: String



17
18
19
# File 'lib/avo/app/action.rb', line 17

def response
  @response
end

#themeObject

Returns the value of attribute theme.



7
8
9
# File 'lib/avo/app/action.rb', line 7

def theme
  @theme
end

Class Method Details

.add_field(action, field) ⇒ Object



33
34
35
# File 'lib/avo/app/action.rb', line 33

def add_field(action, field)
  @@fields[action].push field
end

.fields(&block) ⇒ Object



24
25
26
27
# File 'lib/avo/app/action.rb', line 24

def fields(&block)
  @@fields[self] ||= []
  yield
end

.get_fieldsObject



29
30
31
# File 'lib/avo/app/action.rb', line 29

def get_fields
  @@fields[self] or []
end

Instance Method Details

#download(path, filename) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/avo/app/action.rb', line 140

def download(path, filename)
  self.response[:type] = :download
  self.response[:path] = path
  self.response[:filename] = filename

  self
end

#fail(text) ⇒ Object



100
101
102
103
104
105
# File 'lib/avo/app/action.rb', line 100

def fail(text)
  self.response[:message_type] = :error
  self.response[:message] = text

  self
end

#get_fieldsObject



148
149
150
# File 'lib/avo/app/action.rb', line 148

def get_fields
  self.class.get_fields
end

#handle_action(request, models, raw_fields) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/avo/app/action.rb', line 67

def handle_action(request, models, raw_fields)
  avo_fields = get_fields.map { |field| [field.id, field] }.to_h

  if raw_fields.present?
    fields = raw_fields.to_unsafe_h.map do |name, value|
      [name, avo_fields[name.to_sym].resolve_attribute(value)]
    end

    fields = fields.to_h
  else
    fields = {}
  end

  result = self.handle request, models, fields

  self
end

#http_redirect(path) ⇒ Object



114
115
116
117
118
119
# File 'lib/avo/app/action.rb', line 114

def http_redirect(path)
  self.response[:type] = :http_redirect
  self.response[:path] = path

  self
end

#idObject



85
86
87
# File 'lib/avo/app/action.rb', line 85

def id
  self.class.name.underscore.gsub '/', '_'
end

#open_in_new_tab(path) ⇒ Object



133
134
135
136
137
138
# File 'lib/avo/app/action.rb', line 133

def open_in_new_tab(path)
  self.response[:type] = :open_in_new_tab
  self.response[:path] = path

  self
end

#redirect(path) ⇒ Object



107
108
109
110
111
112
# File 'lib/avo/app/action.rb', line 107

def redirect(path)
  self.response[:type] = :redirect
  self.response[:path] = path

  self
end

#reloadObject



121
122
123
124
125
# File 'lib/avo/app/action.rb', line 121

def reload
  self.response[:type] = :reload

  self
end

#reload_resourcesObject



127
128
129
130
131
# File 'lib/avo/app/action.rb', line 127

def reload_resources
  self.response[:type] = :reload_resources

  self
end

#render_response(model, resource) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/avo/app/action.rb', line 51

def render_response(model, resource)
  fields = get_fields.map { |field| field.fetch_for_action(model, resource) }

  {
    id: id,
    name: name,
    fields: fields,
    message: message,
    theme: theme,
    confirm_text: confirm_text,
    cancel_text: cancel_text,
    default: default,
    action_class: self.class.to_s,
  }
end

#succeed(text) ⇒ Object



93
94
95
96
97
98
# File 'lib/avo/app/action.rb', line 93

def succeed(text)
  self.response[:message_type] = :success
  self.response[:message] = text

  self
end