Project

General

Profile

Download (10.1 KB) Statistics
| Branch: | Tag: | Revision:
55ed30c7 Ohad Levy
module LayoutHelper
d17ecbc9 Amos Benari
def title(page_title, page_header = nil)
017e1049 Ohad Levy
content_for(:title, page_title.to_s)
c6e02bd3 Joseph Magen
@page_header ||= page_header || @content_for_title || page_title.to_s
55ed30c7 Ohad Levy
end

05ab4c16 Ohad Levy
def title_actions *elements
017e1049 Ohad Levy
content_for(:title_actions) { elements.join(" ").html_safe }
05ab4c16 Ohad Levy
end

e4b5713e Amos Benari
def button_group *elements
8527c9f1 Amos Benari
content_tag(:div,:class=>"btn-group") { elements.join(" ").html_safe }
e4b5713e Amos Benari
end

05ab4c16 Ohad Levy
def search_bar *elements
017e1049 Ohad Levy
content_for(:search_bar) { elements.join(" ").html_safe }
05ab4c16 Ohad Levy
end

55ed30c7 Ohad Levy
def stylesheet(*args)
19f55923 Eric D. Helms
content_for(:stylesheets) { stylesheet_link_tag(*args) }
55ed30c7 Ohad Levy
end

def javascript(*args)
19f55923 Eric D. Helms
content_for(:javascripts) { javascript_include_tag(*args) }
55ed30c7 Ohad Levy
end
05ab4c16 Ohad Levy
d31eea8c Amos Benari
def addClass(options={}, new_class='')
bf4a13d3 Walden Raines
options[:class] = "#{new_class} #{options[:class]}"
end

05ab4c16 Ohad Levy
def text_f(f, attr, options = {})
field(f, attr, options) do
bf4a13d3 Walden Raines
addClass options, "form-control"
05ab4c16 Ohad Levy
f.text_field attr, options
end
end

f8d94608 Amos Benari
def line_count (f, attr)
rows = f.object.try(attr).to_s.lines.count rescue 1
rows == 0 ? 1 : rows
end

05ab4c16 Ohad Levy
def textarea_f(f, attr, options = {})
field(f, attr, options) do
f8d94608 Amos Benari
options[:rows] = line_count(f, attr) if options[:rows] == :auto
bf4a13d3 Walden Raines
addClass options, "form-control"
05ab4c16 Ohad Levy
f.text_area attr, options
end
end

def password_f(f, attr, options = {})
field(f, attr, options) do
0c6065e6 Libor Zoubek
options[:autocomplete] ||= "off"
3595a70c Joseph Mitchell Magen
options[:placeholder] ||= password_placeholder(f.object)
bf4a13d3 Walden Raines
addClass options, "form-control"
05ab4c16 Ohad Levy
f.password_field attr, options
end
end

671b45e9 Joseph Mitchell Magen
def checkbox_f(f, attr, options = {}, checked_value="1", unchecked_value="0")
f8d94608 Amos Benari
text = options.delete(:help_text)
inline = options.delete(:help_inline)
05ab4c16 Ohad Levy
field(f, attr, options) do
bf4a13d3 Walden Raines
help_inline = inline.blank? ? '' : content_tag(:span, inline, :class => "help-block")
f.check_box(attr, options, checked_value, unchecked_value) + " #{text} " + help_inline.html_safe
05ab4c16 Ohad Levy
end
end

671b45e9 Joseph Mitchell Magen
14d225cc Amos Benari
def multiple_checkboxes(f, attr, klass, associations, options = {}, html_options={})
if associations.count > 5
1f1367d6 Amos Benari
associated_obj = klass.send(ActiveModel::Naming.plural(associations.first))
selected_ids = associated_obj.select("#{associations.first.class.table_name}.id").map(&:id)
multiple_selects(f, attr, associations, selected_ids, options, html_options)
14d225cc Amos Benari
else
field(f, attr, options) do
410505f4 Marek Hulan
authorized_edit_habtm klass, associations, options[:prefix], html_options
14d225cc Amos Benari
end
05ab4c16 Ohad Levy
end
end

671b45e9 Joseph Mitchell Magen
# add hidden field for options[:disabled]
1f1367d6 Amos Benari
def multiple_selects(f, attr, associations, selected_ids, options={}, html_options={})
d31eea8c Amos Benari
options.merge!(:size => "col-md-10")
671b45e9 Joseph Mitchell Magen
field(f, attr,options) do
attr_ids = (attr.to_s.singularize+"_ids").to_sym
1fa008a4 Joseph Magen
hidden_fields = ''
html_options["data-useds"] ||= "[]"
JSON.parse(html_options["data-useds"]).each do |disabled_value|
1f1367d6 Amos Benari
hidden_fields += f.hidden_field(attr_ids, :multiple => true, :value => disabled_value, :id=>'' )
671b45e9 Joseph Mitchell Magen
end
1f1367d6 Amos Benari
hidden_fields + f.collection_select(attr_ids, associations.all.sort_by { |a| a.to_s },
c2c32409 Greg Sutcliffe
:id, :to_label ,options.merge(:selected => selected_ids),
1f1367d6 Amos Benari
html_options.merge(:multiple => true))
671b45e9 Joseph Mitchell Magen
end
end

dd42df0a Ohad Levy
def radio_button_f(f, attr, options = {})
text = options.delete(:text)
value = options.delete(:value)
bf4a13d3 Walden Raines
label_tag('', :class=>"radio-inline") do
99527500 Jimmi Dyson
f.radio_button(attr, value, options) + " #{text} "
dd42df0a Ohad Levy
end
end

05ab4c16 Ohad Levy
def select_f(f, attr, array, id, method, select_options = {}, html_options = {})
52667feb Joseph Magen
html_options.merge!(:size => 'col-md-10') if html_options[:multiple]
05ab4c16 Ohad Levy
field(f, attr, html_options) do
bf4a13d3 Walden Raines
addClass html_options, "form-control"
05ab4c16 Ohad Levy
f.collection_select attr, array, id, method, select_options, html_options
end
end

def selectable_f(f, attr, array, select_options = {}, html_options = {})
52667feb Joseph Magen
html_options.merge!(:size => 'col-md-10') if html_options[:multiple]
05ab4c16 Ohad Levy
field(f, attr, html_options) do
bf4a13d3 Walden Raines
addClass html_options, "form-control"
05ab4c16 Ohad Levy
f.select attr, array, select_options, html_options
end
end

0c87d936 Ohad Levy
def file_field_f(f, attr, options = {})
field(f, attr, options) do
f.file_field attr, options
end
end

acfbc458 Marek Hulan
def autocomplete_f(f, attr, options = {})
field(f, attr, options) do
path = options.delete(:path) || send("#{f.object.class.pluralize.underscore}_path")
auto_complete_search(attr,
f.object.send(attr).try(:squeeze, " "),
options.merge(
:placeholder => _("Filter") + ' ...',
:path => path,
8527c9f1 Amos Benari
:name => "#{f.object_name}[#{attr}]"
)
acfbc458 Marek Hulan
).html_safe
end
end

05ab4c16 Ohad Levy
def field(f, attr, options = {})
85eac622 Amos Benari
error = f.object.errors[attr] if f && f.object.respond_to?(:errors)
89b9ecb0 Amos Benari
help_inline = help_inline(options.delete(:help_inline), error)

017e1049 Ohad Levy
help_block = content_tag(:span, options.delete(:help_block), :class => "help-block")
bf4a13d3 Walden Raines
size_class = options.delete(:size) || "col-md-4"
content_tag(:div, :class=> "clearfix") do
8527c9f1 Amos Benari
content_tag :div, :class => "form-group #{error.empty? ? "" : 'has-error'}",
:id => options.delete(:control_group_id) do

a33142b9 Dominic Cleal
label = options[:label] == :none ? '' : options.delete(:label)
8527c9f1 Amos Benari
label ||= ((clazz = f.object.class).respond_to?(:gettext_translation_for_attribute_name) &&
s_(clazz.gettext_translation_for_attribute_name attr)) if f
a33142b9 Dominic Cleal
label = label.present? ? label_tag(attr, label, :class => "col-md-2 control-label") : ''

label.html_safe +
content_tag(:div, :class => size_class) do
yield.html_safe + help_block.html_safe
end.html_safe + help_inline.html_safe
d31eea8c Amos Benari
end.html_safe
bf4a13d3 Walden Raines
end
05ab4c16 Ohad Levy
end

89b9ecb0 Amos Benari
def help_inline(inline, error)
help_inline = error.empty? ? inline : error.to_sentence.html_safe
case help_inline
when blank?
""
when :indicator
bf4a13d3 Walden Raines
content_tag(:span, image_tag('spinner.gif', :class => 'hide'), :class => "help-block help-inline")
89b9ecb0 Amos Benari
else
bf4a13d3 Walden Raines
content_tag(:span, help_inline, :class => "help-block help-inline")
89b9ecb0 Amos Benari
end
end

bfee97b6 Lukas Zapletal
def form_to_submit_id f
object = f.object.respond_to?(:to_model) ? f.object.to_model : f.object
key = object ? (object.persisted? ? :update : :create) : :submit
model = if object.class.respond_to?(:humanize_class_name)
object.class.humanize_class_name.downcase
elsif object.class.respond_to?(:model_name)
object.class.model_name.human.downcase
else
f.object_name.to_s
end.gsub(/\W+/, '_')
"aid_#{key}_#{model}"
end

dd42df0a Ohad Levy
def submit_or_cancel f, overwrite = false, args = { }
ef4b97d1 Joseph Mitchell Magen
args[:cancel_path] ||= send("#{controller_name}_path")
8527c9f1 Amos Benari
content_tag(:div, :class => "clearfix") do
content_tag(:div, :class => "form-actions") do
text = overwrite ? _("Overwrite") : _("Submit")
options = overwrite ? {:class => "btn btn-danger"} : {:class => "btn btn-primary"}
options.merge! :'data-id' => form_to_submit_id(f) unless options.has_key?(:'data-id')
link_to(_("Cancel"), args[:cancel_path], :class => "btn btn-default") + " " +
f.submit(text, options)
end
05ab4c16 Ohad Levy
end
end

def base_errors_for obj
017e1049 Ohad Levy
unless obj.errors[:base].blank?
ede5687c Ohad Levy
alert :header => _("Unable to save"),
:class => 'alert-danger base in fade',
:text => obj.errors[:base].map { |e| '<li>'.html_safe + e + '</li>'.html_safe }.join.html_safe
05ab4c16 Ohad Levy
end
end

2b54d6ef Ohad Levy
def popover title, msg, options = {}
d31eea8c Amos Benari
link_to icon_text("info-sign", title), {}, { :remote => true, :rel => "popover", :data => {"content" => msg, "original-title" => title} }.merge(options)
2b54d6ef Ohad Levy
end

ede5687c Ohad Levy
def will_paginate(collection = nil, options = {})
bf4a13d3 Walden Raines
options.merge!(:class=>"col-md-7")
017e1049 Ohad Levy
options[:renderer] ||= "WillPaginate::ActionView::BootstrapLinkRenderer"
options[:inner_window] ||= 2
options[:outer_window] ||= 0
bf4a13d3 Walden Raines
options[:previous_label] ||= _('&laquo;')
options[:next_label] ||= _('&raquo;')
017e1049 Ohad Levy
super collection, options
end

def page_entries_info(collection, options = {})
1bf7cc35 Amos Benari
html = if collection.total_entries == 0
_("No entries found")
else
if collection.total_pages < 2
n_("Displaying <b>%{count}</b> entry", "Displaying <b>all %{count}</b> entries", collection.total_entries) % {:count => collection.total_entries}
else
_("Displaying entries <b>%{from} - %{to}</b> of <b>%{count}</b> in total") %
{ :from => collection.offset + 1, :to => collection.offset + collection.length, :count => collection.total_entries }
end
end.html_safe
017e1049 Ohad Levy
html += options[:more].html_safe if options[:more]
bf4a13d3 Walden Raines
content_tag(:div, :class=>"col-md-5") do
content_tag(:ul, :class => 'pagination') do
content_tag(:li, link_to(html, "#"), :class=>"pull-left")
end
end
82e47ae0 Amos Benari
end

def form_for(record_or_name_or_array, *args, &proc)
if args.last.is_a?(Hash)
cc68313b Amos Benari
args.last[:html] = {:class=>"form-horizontal well"}.merge(args.last[:html]||{})
82e47ae0 Amos Benari
else
cc68313b Amos Benari
args << {:html=>{:class=>"form-horizontal well"}}
82e47ae0 Amos Benari
end
super record_or_name_or_array, *args, &proc
end

def icons i
bf4a13d3 Walden Raines
content_tag :i, :class=>"glyphicon glyphicon-#{i}" do
82e47ae0 Amos Benari
yield
end
017e1049 Ohad Levy
end

eb56c27a Ohad Levy
def icon_text(i, text="", opts = {})
bf4a13d3 Walden Raines
(content_tag(:i,"", :class=>"glyphicon glyphicon-#{i} #{opts[:class]}") + " " + text).html_safe
dec6f148 Amos Benari
end

f262f77a Ohad Levy
def alert opts = {}
16b9f09b Ohad Levy
opts[:close] = true if opts[:close].nil?
bfbf7ed8 Lukas Zapletal
opts[:header] ||= _("Warning!")
16b9f09b Ohad Levy
opts[:text] ||= _("Alert")
html_class = "alert #{opts[:class]} "
html_class += 'alert-dismissable' if opts[:close]
content_tag :div, :class => html_class do
f262f77a Ohad Levy
result = "".html_safe
result += alert_close if opts[:close]
result += alert_header(opts[:header])
result += opts[:text].html_safe
result
end
end

def alert_header text
8527c9f1 Amos Benari
"<h4 class='alert-heading'>#{text}</h4>".html_safe
f262f77a Ohad Levy
end

def alert_close
ede5687c Ohad Levy
'<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>'.html_safe
f262f77a Ohad Levy
end

55ed30c7 Ohad Levy
end