Module: Qcourses::ViewHelpers

Defined in:
lib/qcourses/view_helpers.rb

Instance Method Summary collapse

Instance Method Details

#course_optionsObject



11
12
13
# File 'lib/qcourses/view_helpers.rb', line 11

def course_options
  CourseRepository.all.collect { |course| [course.identification, course.name] }
end

#display_if_locationsObject



17
18
19
# File 'lib/qcourses/view_helpers.rb', line 17

def display_if_locations
  Location.all.empty? && "display:none;" || "display:block"
end

#display_unless_locationsObject



20
21
22
# File 'lib/qcourses/view_helpers.rb', line 20

def display_unless_locations
  Location.all.empty? && "display:block;" || "display:none"
end


34
35
36
37
# File 'lib/qcourses/view_helpers.rb', line 34

def event_registration_link(event)
  return "closed" unless event.open?
  haml "%a{ :href=> url('/registrations/new/#{event.id}') } register"
end

#human_period(from, to) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/qcourses/view_helpers.rb', line 24

def human_period(from, to)
  if (from.year != to.year)
    "#{from.strftime('%e/%m/%Y').strip} - #{to.strftime('%e/%m/%Y').strip}"
  elsif (from.month != to.month)
    "#{from.strftime('%e/%m')} - #{to.strftime('%e/%m %Y').strip}"
  else
    "#{from.day} - #{to.strftime('%e %B %Y').strip}"
  end
end

#input_for(object, attribute_name, options = {}) ⇒ Object



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

def input_for(object, attribute_name, options={})
  object = instance_variable_get("@#{object}") if object.is_a?(Symbol)
  return '' unless object
  array_element = options.delete :array_element
  klass = options.delete :class
  suppress_error_messages = options.delete :suppress_error_messages
  date_format = options.delete :time_format
  tag_attributes = {:type => 'text', :name => input_name(object, attribute_name, array_element)}
  tag_attributes[:value] = format_value(object.send(attribute_name), date_format)
  tag_classes = []
  tag_classes << 'error' if object.errors.on(attribute_name)
  tag_classes << klass if klass
  tag_attributes[:class] = tag_classes.join(' ') unless tag_classes.empty?
  tag_attributes.merge! options
  result = haml("%input#{tag_attributes.inspect}")
  result += haml("%span{:class => 'error'} #{object.errors.on(attribute_name).first}" ) if object.errors.on(attribute_name) && !suppress_error_messages
  result
end

#location_optionsObject



14
15
16
# File 'lib/qcourses/view_helpers.rb', line 14

def location_options
  Location.all.collect { |location| [location.id, location.name] } + [['0', 'new location']]
end

#options_for_select(options, value) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/qcourses/view_helpers.rb', line 3

def options_for_select options, value
  return '' unless options
  options.collect do |option| 
    tag_attributes = {:value => option[0]}
    tag_attributes[:selected] = "selected" if value.to_s == option[0].to_s
    haml "%option#{tag_attributes.inspect} #{option[1]}", :layout => false
  end.join
end

#select_for(object, attribute_name, opts_for_select = [], options = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/qcourses/view_helpers.rb', line 72

def select_for(object, attribute_name, opts_for_select=[], options={})
  object = instance_variable_get("@#{object}") if object.is_a?(Symbol)
  return '' unless object
  array_element = options.delete :array_element
  klass = options.delete :class
  tag_classes = []
  tag_classes << 'error' if object.errors.on(attribute_name)
  tag_classes << klass if klass
  tag_attributes = {:name => input_name(object, attribute_name, array_element)}
  tag_attributes[:class] = tag_classes.join(' ') unless tag_classes.empty?
  tag_attributes.merge! options
  %Q{<select #{text_hash(tag_attributes)}>
      #{options_for_select(opts_for_select, object.send(attribute_name).to_s)}
     </select>}
end

#textarea_for(object, attribute_name, options = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/qcourses/view_helpers.rb', line 58

def textarea_for(object, attribute_name, options={})
  object = instance_variable_get("@#{object}") if object.is_a?(Symbol)
  return '' unless object
  klass = options.delete :class
  tag_attributes = {:name => input_name(object, attribute_name, options[:array_element])}
  value = object.send(attribute_name) || ''
  tag_classes = []
  tag_classes << 'error' if object.errors.on(attribute_name)
  tag_classes << klass if klass
  tag_attributes[:class] = tag_classes.join(' ') unless tag_classes.empty?
  tag_attributes.merge! options
  "<textarea #{text_hash(tag_attributes)}>#{value}</textarea>"
end