Class: Clot::CollectionSelect

Inherits:
ClotTag
  • Object
show all
Includes:
ModelTag
Defined in:
lib/clot/model_form_tags.rb

Instance Method Summary collapse

Methods included from ModelTag

#render

Methods inherited from ClotTag

#initialize, #render

Methods included from TagHelper

#resolve_value, #split_params

Methods included from AttributeSetter

#set_attributes

Constructor Details

This class inherits a constructor from Clot::ClotTag

Instance Method Details

#gen_option(item) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/clot/model_form_tags.rb', line 118

def gen_option(item)
  selection_string = ""
  item_string = item
  value_string = ""

  # this line below is for BSON::ObjectId which doesn't respond to :id, but does :_id
  @default_id = "_id" if @default_id == 'id' and item.respond_to?(:_id)

  # @item = NationSignupDrop
  # item = NationPlan

  attribute_names = @attribute_name.split('.')

  if item.is_a?(String) || item.is_a?(Fixnum) || item.is_a?(Float)
    if (@item[@attribute_name.to_sym].to_s == item.to_s) || (@item.respond_to?(@attribute_name.to_sym) && @item.send(@attribute_name.to_sym).to_s == item.to_s)
      selection_string = ' selected="selected"'
    end
  else
    item_string = item[@default_name.to_sym] || (@item.respond_to?(@attribute_name.to_sym) && @item.send(@default_name.to_sym))
    value_string = %{ value="#{item[@default_id.to_sym]}"}
    if @item.class.to_s == "NationSignupDrop" and attribute_names.size == 3 and attribute_names[0] == 'payment_profile' # this is a special case just for 3dna nation signup mongo stuff
      if attribute_names[1] == 'billing_address'
        if item[@default_id.to_sym].to_s == @item.source.payment_profile.billing_address[attribute_names[2].to_sym].to_s
          selection_string = ' selected="selected"'
        end
      else
        if item[@default_id.to_sym].to_s == @item.source.payment_profile[attribute_names[1].to_sym][attribute_names[2].to_sym].to_s
          selection_string = ' selected="selected"'
        end
      end
    elsif attribute_names.size == 3
      if item[@default_id.to_sym].to_s == @item[attribute_names[0].to_sym][attribute_names[1].to_sym][attribute_names[2].to_sym].to_s
        selection_string = ' selected="selected"'
      end
    elsif attribute_names.size == 2
      if item[@default_id.to_sym].to_s == @item[attribute_names.first.to_sym][attribute_names.last.to_sym].to_s
        selection_string = ' selected="selected"'
      end
    else
      if item[@default_id.to_sym].to_s == @item[@attribute_name.to_sym].to_s
        selection_string = ' selected="selected"'
      end
    end
  end

  "<option#{value_string}#{selection_string}>#{item_string}</option>"
end

#personal_attributes(name, value) ⇒ Object



166
167
168
169
170
171
# File 'lib/clot/model_form_tags.rb', line 166

def personal_attributes(name,value)
  case name
    when 'prompt' then
      @prompt_option = %{<option value="">#{value}</option>}
  end
end

#render_stringObject



173
174
175
176
177
178
179
180
# File 'lib/clot/model_form_tags.rb', line 173

def render_string
  @option_string = "#{@prompt_option}"
  @collection.each do |item|
    @option_string << gen_option(item)
  end

  %{<select id="#{@id_string}" name="#{@name_string}">#{@option_string}</select>}
end

#set_primary_attributes(context) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/clot/model_form_tags.rb', line 103

def set_primary_attributes(context)
  super context
  if @params[0] && ! @params[0].match(/:/)
     @collection = resolve_value(@params.shift,context)
  end
  @default_id = 'id'
  @default_name = 'name'
  if @params[0] && ! @params[0].match(/:/)
     @default_id = resolve_value(@params.shift,context)
  end
  if @params[0] && ! @params[0].match(/:/)
     @default_name = resolve_value(@params.shift,context)
  end
end