Class: Katapult::WebUI

Inherits:
Element show all
Defined in:
lib/katapult/elements/web_ui.rb

Constant Summary collapse

RAILS_ACTIONS =
%w[ index show new create edit update destroy ]
UnknownActionError =
Class.new(StandardError)
UnknownModelError =
Class.new(StandardError)

Constants inherited from Element

Element::UnknownFormattingError, Element::UnknownOptionError

Instance Attribute Summary collapse

Attributes inherited from Element

#application_model, #name, #options

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ WebUI

Returns a new instance of WebUI.



18
19
20
21
22
# File 'lib/katapult/elements/web_ui.rb', line 18

def initialize(*args)
  self.actions = []

  super
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



12
13
14
# File 'lib/katapult/elements/web_ui.rb', line 12

def actions
  @actions
end

Instance Method Details

#action(name, options = {}) ⇒ Object

DSL



25
26
27
28
29
30
# File 'lib/katapult/elements/web_ui.rb', line 25

def action(name, options = {})
  actions << Action.new(:new, options) if name.to_s == 'create'
  actions << Action.new(:edit, options) if name.to_s == 'update'

  actions << Action.new(name, options)
end

#crudObject

DSL



33
34
35
# File 'lib/katapult/elements/web_ui.rb', line 33

def crud
  i(index show create update destroy).each &method(:action)
end

#crud_only?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/katapult/elements/web_ui.rb', line 37

def crud_only?
  actions.map(&:name).sort == RAILS_ACTIONS.sort
end

#custom_actionsObject



52
53
54
# File 'lib/katapult/elements/web_ui.rb', line 52

def custom_actions
  actions.reject { |a| RAILS_ACTIONS.include? a.name }
end

#find_action(action_name) ⇒ Object



56
57
58
# File 'lib/katapult/elements/web_ui.rb', line 56

def find_action(action_name)
  actions.find { |a| a.name == action_name.to_s }
end

#modelObject



41
42
43
44
# File 'lib/katapult/elements/web_ui.rb', line 41

def model
  model_name = @model || self.name
  application_model.get_model! model_name
end

#model_name(kind = nil) ⇒ Object



77
78
79
# File 'lib/katapult/elements/web_ui.rb', line 77

def model_name(kind = nil)
  model.name(kind)
end

#paramsObject



46
47
48
49
50
# File 'lib/katapult/elements/web_ui.rb', line 46

def params
  model.attrs.map do |attr|
    attr.name :symbol
  end
end

#path(action, object_name = nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/katapult/elements/web_ui.rb', line 60

def path(action, object_name = nil)
  unless action.is_a?(Action)
    not_found_message = "Unknown action '#{action}'"
    action = find_action(action) or raise UnknownActionError, not_found_message
  end

  member_path = "#{model.name(:variable)}_path"
  collection_path = "#{model.name(:variables)}_path"

  path = ''
  path << action.name << '_' unless %w[index show destroy].include?(action.name)
  path << (action.member? ? member_path : collection_path)
  path << "(#{object_name})" if object_name

  path
end

#render(options = {}) ⇒ Object



81
82
83
# File 'lib/katapult/elements/web_ui.rb', line 81

def render(options = {})
  Generators::WebUIGenerator.new(self, options).invoke_all
end