Class: Nemo::Examples::PersonEditor::Root

Inherits:
Wee::Component
  • Object
show all
Defined in:
lib/nemo/examples/person_editor.rb

Overview

Display a list of persons for editing or deletion

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRoot

Create a ListItemViewer for each record



123
124
125
126
127
# File 'lib/nemo/examples/person_editor.rb', line 123

def initialize
  super
  @views = Wee::Session.current.domainmodel.persons.collect { |p| viewer_for(p) }
  @views.each { |view| @children << view }
end

Instance Attribute Details

#viewsObject

Returns the value of attribute views.



119
120
121
# File 'lib/nemo/examples/person_editor.rb', line 119

def views
  @views
end

Instance Method Details

#add_newObject

Add a new Person



176
177
178
179
180
181
# File 'lib/nemo/examples/person_editor.rb', line 176

def add_new
  if obj = call( Nemo::Examples::PersonEditor::Editor.new(Nemo::Examples::PersonEditor::Person.new.metaobject) )
    session.domainmodel.add_person(obj)
    @views << viewer_for(obj)
  end
end

#backtrack_state(snapshot) ⇒ Object

Enable Wee backtracking for views



131
132
133
134
# File 'lib/nemo/examples/person_editor.rb', line 131

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

#delete(obj) ⇒ Object

Delete a Person. Requires confirmation: Are you sure you want to delete? [OK] [Cancel]



192
193
194
195
196
197
198
199
200
201
# File 'lib/nemo/examples/person_editor.rb', line 192

def delete(obj)
  if call( Nemo::Components::ConfirmDialog.new("Are you sure you want to delete #{obj.name}?") )
    session.domainmodel.remove_person(obj)
    session.domainmodel.persons.each do |person|
      person.father = nil if person.father == obj
      person.mother = nil if person.mother == obj
    end
    @views.delete_if { |view| view.metaobject.baseobject == obj }
  end
end

#edit(obj) ⇒ Object

Edit a Person



185
186
187
# File 'lib/nemo/examples/person_editor.rb', line 185

def edit(obj)
  call( Nemo::Examples::PersonEditor::Editor.new(obj.metaobject) ) # will auto-commit
end

#renderObject

Render the page



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/nemo/examples/person_editor.rb', line 144

def render
  r.html do
    r.head { r.title('Person Editor') }
    r.link.type('text/css').rel('stylesheet').href('/nemo/person_editor.css')
    r.body do
      r.h1('Person Editor')
      r.table.width('100%').with do
        if @views.empty?
          r.table_row { r.table_header('No persons found.') }
        else
          labels = @views.first.metaobject.visible_attributes.collect { |a| a.label }
          r.table_row { labels.each { |label| r.table_header(label) }; r.table_header { r.space } }
          @views.each do |view|
            r.table_row do
              r.render(view)
              r.table_header do
                r.anchor.callback{ edit(view.metaobject.baseobject) }.with('edit')
                r.space; r.text('|'); r.space
                r.anchor.callback{ delete(view.metaobject.baseobject) }.with('delete')
              end
            end
          end
        end
      end
      r.break
      r.anchor.callback{ add_new }.with('create new person')
    end
  end
end

#viewer_for(obj) ⇒ Object

Create a new ListItemViewer for a given metaobject



138
139
140
# File 'lib/nemo/examples/person_editor.rb', line 138

def viewer_for(obj)
  Nemo::Examples::PersonEditor::ListItemViewer.new(obj.metaobject.hide!(:password, :interests))
end