Module: Qcourses::ViewHelpers
- Defined in:
- lib/qcourses/view_helpers.rb
Instance Method Summary collapse
- #course_options ⇒ Object
- #display_if_locations ⇒ Object
- #display_unless_locations ⇒ Object
- #event_registration_link(event) ⇒ Object
- #human_period(from, to) ⇒ Object
- #input_for(object, attribute_name, options = {}) ⇒ Object
- #location_options ⇒ Object
- #options_for_select(options, value) ⇒ Object
- #select_for(object, attribute_name, opts_for_select = [], options = {}) ⇒ Object
- #textarea_for(object, attribute_name, options = {}) ⇒ Object
Instance Method Details
#course_options ⇒ Object
11 12 13 |
# File 'lib/qcourses/view_helpers.rb', line 11 def CourseRepository.all.collect { |course| [course.identification, course.name] } end |
#display_if_locations ⇒ Object
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_locations ⇒ Object
20 21 22 |
# File 'lib/qcourses/view_helpers.rb', line 20 def display_unless_locations Location.all.empty? && "display:block;" || "display:none" end |
#event_registration_link(event) ⇒ Object
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, ={}) object = instance_variable_get("@#{object}") if object.is_a?(Symbol) return '' unless object array_element = .delete :array_element klass = .delete :class = .delete :suppress_error_messages date_format = .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! result = haml("%input#{tag_attributes.inspect}") result += haml("%span{:class => 'error'} #{object.errors.on(attribute_name).first}" ) if object.errors.on(attribute_name) && ! result end |
#location_options ⇒ Object
14 15 16 |
# File 'lib/qcourses/view_helpers.rb', line 14 def 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 , value return '' unless .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=[], ={}) object = instance_variable_get("@#{object}") if object.is_a?(Symbol) return '' unless object array_element = .delete :array_element klass = .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! %Q{<select #{text_hash(tag_attributes)}> #{(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, ={}) object = instance_variable_get("@#{object}") if object.is_a?(Symbol) return '' unless object klass = .delete :class tag_attributes = {:name => input_name(object, attribute_name, [: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! "<textarea #{text_hash(tag_attributes)}>#{value}</textarea>" end |