Class: Clot::NumberedTag

Inherits:
ClotTag
  • Object
show all
Defined in:
lib/clot/date_tags.rb

Instance Method Summary collapse

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

#can_show(val) ⇒ Object



16
17
18
# File 'lib/clot/date_tags.rb', line 16

def can_show(val)
  true
end

#default_endObject



68
69
70
# File 'lib/clot/date_tags.rb', line 68

def default_end
  59
end

#default_startObject



64
65
66
# File 'lib/clot/date_tags.rb', line 64

def default_start
  0
end

#get_options(from_val, to_val, selected_value = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/clot/date_tags.rb', line 20

def get_options(from_val,to_val, selected_value = nil)
  options = ""

  if from_val < to_val
    range = (from_val..to_val)
  else
    range = (to_val..from_val).to_a.reverse
  end

  range.each do |val|
    if can_show(val)
      if selected_value == val
        options << %{<option selected="selected" value="#{val}">#{value_string(val)}</option>}
      else
        options << %{<option value="#{val}">#{value_string(val)}</option>}
      end
    end
  end
  options
end

#id_string(field_name) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/clot/date_tags.rb', line 72

def id_string(field_name)
  @id_string || if field_name && ! field_name.blank?
    %{id="#{@prefix || 'date'}_#{field_name}"}
  else
    %{id="#{@prefix || 'date'}"}
  end
end

#name_string(field_name) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/clot/date_tags.rb', line 80

def name_string(field_name)
  @name_string || if field_name && ! field_name.blank?
    %{name="#{@prefix || 'date'}[#{field_name}]"}
  else
    %{name="#{@prefix || 'date'}"}
  end
end

#personal_attributes(name, value) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/clot/date_tags.rb', line 48

def personal_attributes(name,value)
  case name
    when "id_string_val" then
      @id_string = %{id="#{value}"}
    when "name_string_val" then
      @name_string = %{name="#{value}"}
    when "field_name" then
      @field_name = value
    when "prefix" then
      @prefix = value
    when "prompt" then
      prompt_text = (value === true) ? default_field_name.pluralize.capitalize : value
      @prompt_val = "<option value=\"\">#{prompt_text}</option>"
  end
end

#render_stringObject



88
89
90
91
# File 'lib/clot/date_tags.rb', line 88

def render_string
  field_name = @field_name || default_field_name
  %{<select #{id_string(field_name)} #{name_string(field_name)}>#{@prompt_val}} + get_options(default_start, default_end, @value_string) + "</select>"
end

#set_primary_attributes(context) ⇒ Object



41
42
43
44
45
46
# File 'lib/clot/date_tags.rb', line 41

def set_primary_attributes(context)
  @value_string = resolve_value(@params.shift,context)
  if @value_string.is_a? Time
    @value_string = @value_string.send(time_method)
  end
end

#time_methodObject



93
94
95
# File 'lib/clot/date_tags.rb', line 93

def time_method
  default_field_name
end

#value_string(val) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/clot/date_tags.rb', line 8

def value_string(val)
  if val < 10
   "0#{val}"
  else
    val
  end
end