Module: RightRails::Helpers::Misc

Defined in:
lib/right_rails/helpers/misc.rb

Overview

Misc view helpers for RightJS

Instance Method Summary collapse

Instance Method Details

#autocomplete_result(entries, *args) ⇒ Object Also known as: autocompleter_result

the autocompletion list result

USAGE:

it might work in several ways

autocomplete_result(list_of_strings)
autocomplete_result(list_of_strings, :highlight => 'search')
autocomplete_result(list_of_strings, :highlight => 'search', :escape => false)

autocomplete_result(list_of_objects, method)
autocomplete_result(list_of_objects, method, :highlight => 'search')


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/right_rails/helpers/misc.rb', line 31

def autocomplete_result(entries, *args)
  return if entries.empty?

  options   = args.last.is_a?(Hash) ? args.pop : {}
  highlight = options[:highlight]
  escape    = options[:escape].nil? ? true : options[:escape]
  field     = args.first


  items     = entries.collect{ |entry|
    entry = entry.send(field) if field
    text  = highlight ? highlight(entry, highlight) : escape ? ERB::Util.html_escape(entry) : entry

     :li, RightRails::Helpers.html_safe(text)
  }.join("")

   :ul, RightRails::Helpers.html_safe(items)
end

#flashesObject

Just a simple flashes generator, might be replaced in the application



8
9
10
11
12
13
14
15
# File 'lib/right_rails/helpers/misc.rb', line 8

def flashes
  items = flash.collect{ |key, text|
    (:div, text, :class => key)
  }.sort.join("")

  (:div, RightRails::Helpers.html_safe(items),
    :id => :flashes, :style => (flash.empty? ? 'display: none' : nil))
end

Generates a link that whil load the refered address in a lightbox

USAGE:

Same as the #link_to method, plus you might specify the :roadtrip argument
to make it a link to a lightbox roadtrip

<%= link_to_lightbox image_tag('/image.thmb'), '/image.full', :roadtrip => true %>


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/right_rails/helpers/misc.rb', line 62

def link_to_lightbox(name, url={}, options={}, &block)
  rightjs_require_module 'lightbox'

  group = options.delete(:roadtrip) || options.delete(:group)

  if RightRails::Config.rightjs_version < 2
    options[:rel] = 'lightbox'
    options[:rel] << "[roadtrip]" if group
  else
    options[:group] = group.to_s if group
  end

  RightRails::Helpers.add_unit_options(options, 'lightbox')

  link_to name, url, options, &block
end

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

The resizable unit helper

USAGE:

<% resizable(:direction => 'bottom', :minHeight => '100px') do %>
  Some content in here
<% end -%>


180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/right_rails/helpers/misc.rb', line 180

def resizable(options={}, &block)
  rightjs_require_module 'resizable'

  RightRails::Helpers.add_unit_options(options, 'resizable')

  options.delete('data-resizable') if options['data-resizable'] == "{}"

  options[:class] ||= ''
  options[:class] << " #{RightRails::Helpers.css_prefix}-resizable#{options[:direction] ? "-#{options.delete(:direction)}" : ''}"
  options[:class].strip!

  content = (:div, (
      (:div, RightRails::Helpers.html_safe(capture(&block)),
        :class => "#{RightRails::Helpers.css_prefix}-resizable-content") +
      (:div, '', :class => "#{RightRails::Helpers.css_prefix}-resizable-handle")
    ), options
  )

  defined?(Rails) && Rails::VERSION::MAJOR < 3 ? concat(content) : content
end

#tab(title, options = {}, &block) ⇒ Object



162
163
164
165
166
167
168
169
170
# File 'lib/right_rails/helpers/misc.rb', line 162

def tab(title, options={}, &block)
  options[:id] = "tab-#{rand.to_s.split('.').last}" if !options[:id] && !options[:url]

  @__tabs << {
    :title   => title,
    :options => options,
    :content => block_given? ? capture(&block) : nil
  }
end

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

Tabs container generator

USAGE:

 <% tabs do %>
   <% tab "Tab 1", :id => :my-tab-1 do %>
     content for tab 1
   <% end -%>
   <% tab "Tab 2", :url => tab2_path %>
 <% end -%>

You also can use the :type option with :carousel or :harmonica value
and you can pass along any standard Tabs unit options along with it

 <% tabs :type => :carousel, :url => '/tabs/%{id}', :cache => true do %>
    <% tab image_tag(image1.thumb_url), :id => image1.id %>
    <% tab image_tag(image2.thumb_url), :id => image2.id %>


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/right_rails/helpers/misc.rb', line 98

def tabs(options={}, &block)
  rightjs_require_module 'tabs'
  @__tabs = []
  yield()

  options.stringify_keys!

  tabs_type = options.delete('type')
  options['id'] = options.delete('id') || "tabs-#{rand.to_s.split('.').last}"

  # checking for the carousel class
  if tabs_type == :carousel
    options['class'] ||= ''
    options['class'] << (options['class'] == '' ? '' : ' ') + "#{RightRails::Helpers.css_prefix}-tabs-carousel"
  end

  # extracting the tab id prefix option
  tab_id_prefix = options[:idPrefix] || options['idPrefix'] || options[:id_prefix] || options['id_prefix'] || ''

  RightRails::Helpers.add_unit_options(options, 'tabs')

  tabs_widget_options = options.delete('data-tabs') || options.delete('data-tabs-options')

  # simple tabs and carousels generator
  content = if tabs_type != :harmonica
    # tabs list
    tabs_list = (:ul,
      RightRails::Helpers.html_safe(
        @__tabs.collect{ |tab|
          (:li, (:a, tab[:title],
            :href => tab[:options][:id] ? "##{tab[:options][:id]}" : tab[:options][:url]
          ))
        }.join("\n")
      )
    ) + "\n";

    # contents list
    bodies_list = @__tabs.collect{|tab|
      tab[:content] ? (:li, tab[:content], :id => "#{tab_id_prefix}#{tab[:options][:id]}") + "\n" : ''
    }.join("")

    (:ul, tabs_list + RightRails::Helpers.html_safe(bodies_list), options)
  else
  # the harmonicas generator
    (:dl,
      RightRails::Helpers.html_safe(
        @__tabs.collect{ |tab|
          (:dt, (:a, tab[:title],
            :href => tab[:options][:id] ? "##{tab[:options][:id]}" : tab[:options][:url]
          )) + "\n" +
          (:dd, tab[:content] || '', :id => tab[:options][:id] ? "#{tab_id_prefix}#{tab[:options][:id]}" : nil)
        }.join("\n")
      ), options
    )

  end

  content = content + RightRails::Helpers.html_safe("\n") + javascript_tag(
    "new Tabs('#{options['id']}', #{tabs_widget_options || '{}'});"
  )

  defined?(Rails) && Rails::VERSION::MAJOR < 3 ? concat(content) : content
end