Module: TJBootstrapHelper::Helper

Defined in:
lib/tj_bootstrap_helper/helper.rb

Instance Method Summary collapse

Instance Method Details

Just like link_to, but wrap with li tag and set class=“active” to corresponding page.



42
43
44
45
46
47
48
49
# File 'lib/tj_bootstrap_helper/helper.rb', line 42

def nav_li *args, &block
  options = (block_given? ? args.first : args.second) || {}
  url = url_for(options)
  active = "active" if url == request.path || url == request.url
   :li, :class => active do
    link_to *args, &block
  end
end

#notice_messageObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/tj_bootstrap_helper/helper.rb', line 7

def notice_message
  flash_messages = []
  flash.each do |type, message|
    type = case type
    when :notice then :success
    when :alert then :error
    end
    content =  :div, :class => "alert fade in alert-#{type}" do
      button_tag("x", :class => "close", "data-dismiss" => "alert") + message
    end
    flash_messages << content if message.present?
  end
  flash_messages.join("\n").html_safe
end

#page_header(*args, &block) ⇒ Object

title, size = 1

size = 1, &block



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tj_bootstrap_helper/helper.rb', line 25

def page_header *args, &block
  if block_given?
    size = (1..6) === args.first ? args.first : 1
     :div, :class => "page-header" do
       "h#{size}" do
        capture(&block)
      end
    end
  else
    title = args.first
    size = (1..6) === args.second ? args.second : 1
     :div, ("h#{size}", title), :class => "page-header"
  end
end

#record_message(object) ⇒ Object



3
4
5
# File 'lib/tj_bootstrap_helper/helper.rb', line 3

def record_message object
  error_messages_for object, :class=>"alert alert-block alert-error", :header_tag=>"h4"
end

#spans(resources, options = {}, &block) ⇒ Object

Options

span

integer between 1..12. Item size.

slice

integer between 1..12. Items per slice (row).

fluid

boolean. Whether the div tag is fluid.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/tj_bootstrap_helper/helper.rb', line 55

def spans resources, options = {}, &block
  return unless resources.is_a?(Array) || resources.is_a?(ActiveRecord::Relation)
  options[:span] ||= 4
  options[:slice] ||= 12/options[:span]
  options[:fluid] ||= false
  klass = options[:fluid] ? "row-fluid" : "row"
  ret = "".html_safe

  resources.each_slice(options[:slice]) do |slice|
    ret +=  :div, :class=>klass do
      slice.inject("".html_safe) do |sum, resource|
        send_args = resource.is_a?(ActiveRecord::Base) ? [:content_tag_for, :div, resource] : [:content_tag, :div]
        send_args << {:class => "span#{options[:span]}"}
        sum + send(*send_args) do
          capture(resource, &block)
        end
      end
    end
  end
  ret
end

#thumbs(resources, image_url_method, options = {}, &block) ⇒ Object

Options

span

integer between 1..12. Item size.

slice

integer between 1..12. Items per slice.

url_method

string or symbol. URL method of items, default is resources’ RESTful URL.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/tj_bootstrap_helper/helper.rb', line 81

def thumbs resources, image_url_method, options = {}, &block
  return unless resources.is_a?(Array) || resources.is_a?(ActiveRecord::Relation)
  options[:span] ||= 4
  options[:slice] ||= 12/options[:span]
  ret = "".html_safe

  resources.each_slice(options[:slice]) do |slice|
    ret +=  :ul, :class => "thumbnails" do
      slice.inject("".html_safe) do |sum, resource|
        img_url = resource.send(image_url_method)
        url = options[:url_method] ? resource.send(options[:url_method]) : url_for(resource)
        send_args = resource.is_a?(ActiveRecord::Base) ? [:content_tag_for, :li, resource] : [:content_tag, :li]
        send_args << {:class => "span#{options[:span]}"}
        sum + send(*send_args) do
          if block_given?
             :div, :class => "thumbnail" do
              image_tag(img_url) + capture(resource, &block)
            end
          else
            link_to image_tag(img_url), url, :class => "thumbnail"
          end
        end
      end
    end
  end
  ret
end