@suggestions   @rss   @archive   @codeforpeople.com     @random   @radio[:m3u|:pls|:ruby]   @family   @neighbors  @twitter 



For Marcel.

Controller.

class IndexController < ApplicationController
  def index
    render :text => [Employee, Boss, Company].inspect
  end

  def field_example_a 
  # take a copy of the auto-generated employee name field and configure it inline, adding a width property
    @field = Employee.field(name).configured{ width 42 }
  # fields must have there controller set in order to render
    @field.controller self
    @field.value = forty-two 
  # see app/views/index/field_example_a.rhtml
  # see app/views/magnetic/employee/name.rhtml
    render
  end

  def field_example_b 
  # take a copy of the auto-generated, but configured, company name field, see app/models/company.rb
    @field = Company.field name
  # fields must have there controller set in order to render
    @field.controller self
    @field.value = forty-two 
  # see app/views/index/field_example_b.rhtml
  # see app/views/magnetic/company/name.rhtml
    render
  end

  class C
    field foo do
      width 42
      height 42.0
    end
  end

  def field_example_c 
  # here we use our own user defined field, hung off of the C class
    @field = C.field foo
  # fields must have there controller set in order to render
    @field.controller self
    @field.value = forty-two 
  # see app/views/index/field_example_c.rhtml
  # see app/views/magnetic/index_controller/c/foo.rhtml
    render
  end

  def interface_example_a
  # load the employee table
    records = Employee.find :all

  # an interface is an interface __too__ a set/tree of active records which
  # maps fields onto values.  the key in the mapping describes the path to the
  # values, the value in the mapping is the field itself.  each value is
  # extended with a ‘field’ method which will lazily load the specififed field
  # and return it.  it’s important to realize that __all__ fields using a
  # given field share that instance and that it is only loaded on demand.
    @interface =
      interface records do
        map name                    => Employee.field(name)
        map company[*].name         => Company.field(name)
        map company[*].boss[*].name => Boss.field(name)
      end

    render
  end
end

View

<dl>

  <% @interface.records.each do |record| %>
    <dt> name </dt> <dd> <%= record.name.field %> </dd>

    <dt> company </dt>
      <dd>
        <dl>
          <% record.company.each do |company| %>
            <dt> name </dt> <dd> <%= company.name.field %> </dd>
          <% end %>
        </dl>
      </dd>

  <% end %>

</dl>

Model extending an auto-field

class Company < ActiveRecord::Base
  has_and_belongs_to_many :employee
  has_and_belongs_to_many :boss

# we extend the auto-generated ‘name’ field using the :extend keyword
  field name, :extend do
    height 42
  end
end

Field view

<!— note that field or @field is always the current field in templates —>
name : <%= field.name %> <hr>
value : <%= @field.value %> <hr>
height : <%= @field.height %> <hr>
controller.class : <%= field.controller.class %> <hr>
Comments (View)
blog comments powered by Disqus