Class: Nemo::Visitors::Editor

Inherits:
Visitor
  • Object
show all
Defined in:
lib/nemo/visitors/editor.rb

Direct Known Subclasses

Examples::PersonEditor::Editor

Instance Attribute Summary collapse

Attributes inherited from Visitor

#metaobject

Instance Method Summary collapse

Methods included from Util::Accessors

#bool_accessor, #call_accessor, #proc_accessor

Constructor Details

#initialize(metaobject) ⇒ Editor

Returns a new instance of Editor.



6
7
8
9
10
11
# File 'lib/nemo/visitors/editor.rb', line 6

def initialize(metaobject)
  super
  @subform = false
  @validation_errors = Array.new
  initialize_subforms
end

Instance Attribute Details

#multiple_attribute_editorsObject

Returns the value of attribute multiple_attribute_editors.



3
4
5
# File 'lib/nemo/visitors/editor.rb', line 3

def multiple_attribute_editors
  @multiple_attribute_editors
end

#validation_errorsObject

Returns the value of attribute validation_errors.



3
4
5
# File 'lib/nemo/visitors/editor.rb', line 3

def validation_errors
  @validation_errors
end

Instance Method Details

#backtrack_state(snapshot) ⇒ Object



41
42
43
44
# File 'lib/nemo/visitors/editor.rb', line 41

def backtrack_state(snapshot)
  super
  snapshot.add(self.children)
end

#calendar_classObject



17
18
19
# File 'lib/nemo/visitors/editor.rb', line 17

def calendar_class
  Nemo::Components::MiniCalendar
end

#call_date_selector(a) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/nemo/visitors/editor.rb', line 65

def call_date_selector(a)
  current = (a.date_from_cache || Date.today)
  if date = call( calendar_class.new(current) )
    a.date_to_cache(date)
    remove_errors(a)
    if error = a.validate_cache
      @validation_errors << error
    end
  end
end

#cancelObject



50
51
52
# File 'lib/nemo/visitors/editor.rb', line 50

def cancel
  answer nil
end

#childrenObject



37
38
39
# File 'lib/nemo/visitors/editor.rb', line 37

def children
  @multiple_attribute_editors.values
end

#commitObject



46
47
48
# File 'lib/nemo/visitors/editor.rb', line 46

def commit
  @metaobject.commit_cache
end

#initialize_subformsObject



21
22
23
24
25
26
# File 'lib/nemo/visitors/editor.rb', line 21

def initialize_subforms
  @multiple_attribute_editors = Hash.new
  @metaobject.multiple_attributes.each do |a|
    @multiple_attribute_editors[a] = multiattr_class.new(a)
  end
end

#multiattr_classObject



13
14
15
# File 'lib/nemo/visitors/editor.rb', line 13

def multiattr_class
  Nemo::Components::MultipleAttributeContainer
end

#remove_errors(a) ⇒ Object



61
62
63
# File 'lib/nemo/visitors/editor.rb', line 61

def remove_errors(a)
  @validation_errors.delete_if { |e| e.attribute == a }
end

#renderObject



86
87
88
89
90
91
92
# File 'lib/nemo/visitors/editor.rb', line 86

def render
  if subform?
    render_main
  else
    r.form.id(@metaobject.object_id).with { render_main }
  end
end

#render_buttonsObject



76
77
78
79
80
81
82
83
84
# File 'lib/nemo/visitors/editor.rb', line 76

def render_buttons
  r.table_row do
    r.table_data.colspan(2).with do
      r.submit_button.id(@metaobject.object_id.to_s+'_save').value('save').callback{ save }
      r.space
      r.submit_button.id(@metaobject.object_id.to_s+'_cancel').value('cancel').callback{ cancel }
    end
  end
end

#render_fieldsObject



109
110
111
# File 'lib/nemo/visitors/editor.rb', line 109

def render_fields
  @metaobject.accept(self)
end

#render_label(a, &block) ⇒ Object



94
95
96
97
98
99
# File 'lib/nemo/visitors/editor.rb', line 94

def render_label(a, &block)
  r.table_row do
    r.table_header(a.label)
    r.table_data { block ? block.call : r.space }
  end
end

#render_mainObject



101
102
103
104
105
106
107
# File 'lib/nemo/visitors/editor.rb', line 101

def render_main
  r.table do
    render_validation_errors
    render_fields
    render_buttons
  end
end

#render_validation_errorsObject



113
114
115
116
117
118
# File 'lib/nemo/visitors/editor.rb', line 113

def render_validation_errors
  if validation_errors?
    r.table_row { r.table_header.colspan(2).with('The following fields are not valid. Please correct.') }
    r.table_row { r.table_data.colspan(2).with { @validation_errors.each { |e| r.text('&bull;&nbsp;'); r.encode_text(e.to_s); r.break } } }
  end
end

#saveObject



54
55
56
57
58
59
# File 'lib/nemo/visitors/editor.rb', line 54

def save
  if valid?
    commit
    answer @metaobject.baseobject
  end
end

#valid?Boolean

Returns:

  • (Boolean)


28
29
30
31
# File 'lib/nemo/visitors/editor.rb', line 28

def valid?
  @validation_errors = @metaobject.validate
  @validation_errors.empty?
end

#validation_errors?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/nemo/visitors/editor.rb', line 33

def validation_errors?
  @validation_errors.size > 0
end

#visit_boolean_attribute(a) ⇒ Object



150
151
152
153
154
155
156
157
# File 'lib/nemo/visitors/editor.rb', line 150

def visit_boolean_attribute(a)
  return if a.read_only?
  render_label(a) do
    r.select_list(a.items).id(a.object_id).labels(a.formatted_items).selected(a.cache).callback do |input|
      @metaobject[a.symbol].cache = Nemo::Util.to_bool(input)
    end
  end
end

#visit_date_attribute(a) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'lib/nemo/visitors/editor.rb', line 140

def visit_date_attribute(a)
  return if a.read_only?
  date = a.cache
  render_label(a) do
    r.text_input.id(a.object_id).maxlength(a.maxlength).value(a.cache).callback { |input| @metaobject[a.symbol].cache = input }
    r.space
    r.nemo_calendar_button.callback { call_date_selector(a) }
  end
end

#visit_multiple_attribute(a) ⇒ Object



159
160
161
162
163
164
# File 'lib/nemo/visitors/editor.rb', line 159

def visit_multiple_attribute(a)
  return if a.read_only?
  render_label(a) do
    r.render( @multiple_attribute_editors[a] )
  end
end

#visit_multiple_relationship_attribute(a) ⇒ Object



175
176
177
178
179
180
181
182
183
184
# File 'lib/nemo/visitors/editor.rb', line 175

def visit_multiple_relationship_attribute(a)
  return if a.read_only?
  items = a.items
  size = (items.size < 8) ? items.size : 8
  render_label(a) do
    r.select_list(a.items).id(a.object_id).size(size).multiple.labels(a.formatted_items).selected(a.cache).callback do |input|
      @metaobject[a.symbol].cache = input
    end
  end
end

#visit_password_attribute(a) ⇒ Object



131
132
133
134
135
136
137
138
# File 'lib/nemo/visitors/editor.rb', line 131

def visit_password_attribute(a)
  return if a.read_only?
  render_label(a) do
    r.text_input.id(a.object_id).type('password').maxlength(a.maxlength).value(a.cache).callback { |input| @metaobject[a.symbol].cache = input }
    r.break
    r.text_input.id(a.object_id.to_s+'_confirm').type('password').maxlength(a.maxlength).value(a.cache_confirm).callback { |input| @metaobject[a.symbol].cache_confirm = input }
  end
end

#visit_single_relationship_attribute(a) ⇒ Object



166
167
168
169
170
171
172
173
# File 'lib/nemo/visitors/editor.rb', line 166

def visit_single_relationship_attribute(a)
  return if a.read_only?
  render_label(a) do
    r.select_list(a.items).id(a.object_id).labels(a.formatted_items).selected(a.cache).callback do |input|
      @metaobject[a.symbol].cache = input
    end
  end
end

#visit_text_attribute(a) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'lib/nemo/visitors/editor.rb', line 120

def visit_text_attribute(a)
  return if a.read_only?
  render_label(a) do
    if a.multiline?
      r.textarea.id(a.object_id).callback{ |input| @metaobject[a.symbol].cache = input }.with { r.encode_text(a.cache) }
    else
      r.text_input.id(a.object_id).maxlength(a.maxlength).value(a.cache).callback { |input| @metaobject[a.symbol].cache = input }
    end
  end
end