Class: RubyMVC::Views::ActiveRecordModelEditor

Inherits:
View
  • Object
show all
Defined in:
lib/ruby_mvc/views/ar_model_editor.rb

Overview

This class is responsible for building a form for the given ActiveRecord model

Constant Summary collapse

DEFAULTS =
{ :label_width => 0.3 }.freeze

Instance Attribute Summary

Attributes inherited from View

#controller, #widget

Instance Method Summary collapse

Methods inherited from View

create_widget, #method_missing, #peer, widget, widget_def

Constructor Details

#initialize(model, options = {}, &block) ⇒ ActiveRecordModelEditor

Returns a new instance of ActiveRecordModelEditor.



35
36
37
38
# File 'lib/ruby_mvc/views/ar_model_editor.rb', line 35

def initialize(model, options = {}, &block)
  super(options, &block)
  load(model, options, &block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RubyMVC::Views::View

Instance Method Details

#createObject



74
75
76
77
78
79
80
# File 'lib/ruby_mvc/views/ar_model_editor.rb', line 74

def create
  if @model
    load(@model.class.new, options)
  else
    alert("No template model.\nUse the #load method instead.")
  end
end

#load(model, options = {}, &block) ⇒ Object

This method is used to load the view with the information in the AR model.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ruby_mvc/views/ar_model_editor.rb', line 43

def load(model, options = {}, &block)
  lwidth = options[:label_width] || DEFAULTS[:label_width]

  stack :width => "100%" do
    flow :width => "100%" do
      model.attributes.keys.sort.each do |key|
        # FIXME: I can't believe that you have to do this
        # this way with AR.  I'm surely missing
        # something...
        m = "#{key}=".to_sym
        value = model.attributes[key]
        flow :width => lwidth, :margin_top => 2 do
          para key, :weight => 'bold', :align => "right"
        end
        flow :width => 1 - lwidth do
          edit_line model.attributes[key] do |e|
            model.send(m, e.text)
          end
        end
      end
    end
  end

  @model = model
  @options = options
end

#saveObject



70
71
72
# File 'lib/ruby_mvc/views/ar_model_editor.rb', line 70

def save
  @model.save!
end