Module: ViewDriver::ViewHelpers

Defined in:
lib/view_driver/view_helpers.rb

Instance Method Summary collapse

Instance Method Details

#classes(*pairs) ⇒ Object

classes is to set css-classes or other attributes conditionally classes(“class-without-conditions”, [“logged-class”, logged_in?], “third-class-without-conditions”)



40
41
42
43
44
45
46
47
48
49
# File 'lib/view_driver/view_helpers.rb', line 40

def classes(*pairs)
  glued_classes = []
  pairs.each do |pair| 
    next if pair.blank?
    arr = Array(pair)
    raise ArgumentError, "css_class or [css_class, condition] are expected (got #{pair.inspect})" if arr.size.zero? || arr.size > 2
    glued_classes << arr[0] if arr[1] || arr.size == 1
  end
  glued_classes.any? ? glued_classes.join(" ") : nil
end

#render_section(section) ⇒ Object

Renders section :section for the current controller and action



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/view_driver/view_helpers.rb', line 5

def render_section(section)
  @sections_collection ||= SectionsCollection.new(@controller)
  
  if RAILS_ENV == "development"
    _time1, _time2 = nil, nil
    _time1 = Time.now
  end
  
  if template = @sections_collection.template_to_render(section)
    result = render(:file => template)
    if RAILS_ENV == "development"
      _time2 = Time.now 
      logger.debug "Rendered section #{template} (#{((_time2 - _time1)*1000).round(1)}ms)"
    end
    result
  elsif RAILS_ENV == "development"
    logger.debug "Missing section #{section}"
    "<!-- Missing section #{section} -->"
  end
end

#yield_with_sublayoutsObject

Renders sublayouts replace <%= yield %> or <%= @content_for_layout %> with it to use sublayouts



28
29
30
31
32
33
34
35
36
# File 'lib/view_driver/view_helpers.rb', line 28

def yield_with_sublayouts
  unless @sublayout.blank?
    sublayout_file = @sublayout.include?('/') ? @sublayout : File.join(SUBLAYOUTS_DIR, @sublayout) 
    RAILS_ENV == 'development' && logger.debug("Rendering template within sublayout #{sublayout_file}")
    render :file => sublayout_file
  else
    @content_for_layout
  end
end