Module: DataMapper::Is::Select

Defined in:
lib/is/select.rb

Overview

dm-is-select

A DataMapper plugin that makes getting the <select> options from a Model easier.

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
IO.read("#{File.dirname(__FILE__)}/../../VERSION").chomp

Instance Method Summary collapse

Instance Method Details

#is_select(select_field = :name, options = {}) ⇒ Object

Defines the field to use for the select menu

Params

  • :field_name => the name of the field values shown in select

  • :options

    • :is_tree => whether if the current Model is an is :tree model. (Defaults to false)

Examples

is :select, :name 
  => creates a <select> options array on the :name attribute of the model 

is :select, :name, :is_tree => true  
    => creates a <select> options array with the results ordered in hierarchical order
       parent > child > grandchild for each parent

is :select, :name, :value_field => :code  
    => creates a <select> options array with the results ordered in hierarchical order
       parent > child > grandchild for each parent

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/is/select.rb', line 39

def is_select(select_field = :name, options = {}) 
  raise ArgumentError, "The :select_field, must be an existing attribute in the Model. Got [ #{select_field.inspect} ]" unless properties.any?{ |p| p.name == select_field.to_sym }
  
  @select_options = {
    :value_field => "id", 
    # add specical features if we are working with Tree Model
    :is_tree => false
  }.merge(options)
  
  @select_field = select_field
  @value_field = @select_options[:value_field]
  
  
  # Add class & Instance methods
  extend  DataMapper::Is::Select::ClassMethods
  # include DataMapper::Is::Select::InstanceMethods
  
end