foreman/app/helpers/application_helper.rb @ 82e47ae0
9fd7478e | Paul Kelly | # Methods added to this helper will be available to all templates in the application
|
|
5563217a | Ohad Levy | module ApplicationHelper
|
|
9161008b | Amos Benari | include HomeHelper
|
|
d7bb0ba7 | Ohad Levy | ||
05ab4c16 | Ohad Levy | protected
|
|
9fd7478e | Paul Kelly | def contract model
|
|
model.to_s
|
|||
end
|
|||
d7bb0ba7 | Ohad Levy | ||
55ed30c7 | Ohad Levy | def show_habtm associations
|
|
render :partial => 'common/show_habtm', :collection => associations, :as => :association
|
|||
end
|
|||
def edit_habtm klass, association
|
|||
9fd7478e | Paul Kelly | render :partial => 'common/edit_habtm', :locals =>{ :klass => klass, :associations => association.all.sort.delete_if{|e| e == klass}}
|
|
55ed30c7 | Ohad Levy | end
|
|
fa7070ca | Ohad Levy | def link_to_remove_fields(name, f)
|
|
82e47ae0 | Amos Benari | f.hidden_field(:_destroy) + link_to_function("x", "remove_fields(this)", :class => "label label-important", :title => "Remove")
|
|
fa7070ca | Ohad Levy | end
|
|
8053eafd | Amos Benari | def trunc text, length
|
|
text = text.to_s
|
|||
options = text.size > length ? {:'data-original-title'=>text, :rel=>'twipsy'} : {}
|
|||
content_tag(:span, truncate(text, :length => length), options).html_safe
|
|||
end
|
|||
9fd7478e | Paul Kelly | # Creates a link to a javascript function that creates field entries for the association on the web page
|
|
# +name+ : String containing links's text
|
|||
# +f+ : FormBuiler object
|
|||
# +association : The field are created to allow entry into this association
|
|||
# +partial+ : String containing an optional partial into which we render
|
|||
e1f867f0 | Ohad Levy | def link_to_add_fields(name, f, association, partial = nil, options = {})
|
|
fa7070ca | Ohad Levy | new_object = f.object.class.reflect_on_association(association).klass.new
|
|
fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
|
|||
render((partial.nil? ? association.to_s.singularize + "_fields" : partial), :f => builder)
|
|||
end
|
|||
82e47ae0 | Amos Benari | link_to_function(name, ("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")").html_safe, options.merge({:class => "btn btn-small btn-success"}) )
|
|
fa7070ca | Ohad Levy | end
|
|
8ba2e00a | Ohad Levy | def toggle_div divs
|
|
9a9b3e75 | Paul Kelly | update_page do |page|
|
|
8ba2e00a | Ohad Levy | (divs.is_a?(Array) ? divs : divs.to_s).each do |div|
|
|
# add jquery '#div' to the div if its missing
|
|||
div = div.to_s
|
|||
div = "##{div}" if div[0] != "#"
|
|||
page << "if ($('#{div}').is(':visible')) {"
|
|||
page[div].hide()
|
|||
page << "} else {"
|
|||
8bf83415 | Ohad Levy | page[div].show
|
|
8ba2e00a | Ohad Levy | page << "}"
|
|
end
|
|||
9a9b3e75 | Paul Kelly | end
|
|
end
|
|||
bd46fa41 | Ohad Levy | def link_to_remove_puppetclass klass
|
|
8053eafd | Amos Benari | options = klass.name.size > 28 ? {:'data-original-title'=>klass.name, :rel=>'twipsy'} : {}
|
|
content_tag(:span, truncate(klass.name, :length => 28), options).html_safe +
|
|||
link_to_function("","remove_puppet_class(this)", :'data-class-id'=>klass.id,
|
|||
:'data-original-title'=>"Click to remove #{klass}", :rel=>'twipsy',
|
|||
24e55891 | Ohad Levy | :class=>"ui-icon ui-icon-minus", "data-type" => type)
|
|
bd46fa41 | Ohad Levy | end
|
|
9a9b3e75 | Paul Kelly | ||
bd46fa41 | Ohad Levy | def link_to_add_puppetclass klass, type
|
|
8053eafd | Amos Benari | options = klass.name.size > 28 ? {:'data-original-title'=>klass.name, :rel=>'twipsy'} : {}
|
|
fdac5d15 | Amos Benari | content_tag(:span, truncate(klass.name, :length => 28), options).html_safe +
|
|
30e1b33e | Ohad Levy | link_to_function("", "add_puppet_class(this)",
|
|
'data-class-id' => klass.id, 'data-type' => type,
|
|||
'data-original-title' => "Click to add #{klass}", :rel => 'twipsy',
|
|||
:class => "ui-icon ui-icon-plus")
|
|||
bd46fa41 | Ohad Levy | end
|
|
9a9b3e75 | Paul Kelly | ||
82e47ae0 | Amos Benari | def check_all_links(form_name=':checkbox')
|
|
link_to_function("Check all", "checkAll('#{form_name}', true)") +
|
|||
link_to_function("Uncheck all", "checkAll('#{form_name}', false)")
|
|||
9fd7478e | Paul Kelly | end
|
|
# Return true if user is authorized for controller/action, otherwise false
|
|||
# +controller+ : String or symbol for the controller
|
|||
# +action+ : String or symbol for the action
|
|||
def authorized_for(controller, action)
|
|||
ad3d61b7 | Paul Kelly | User.current.allowed_to?({:controller => controller.to_s.gsub(/::/, "_").underscore, :action => action}) rescue false
|
|
9fd7478e | Paul Kelly | end
|
|
# Display a link if user is authorized, otherwise a string
|
|||
# +name+ : String to be displayed
|
|||
# +options+ : Hash containing
|
|||
572a19c3 | Paul Kelly | # :controller : String or Symbol representing the controller
|
|
# :auth_action : String or Symbol representing the action to be used for authorization checks
|
|||
9fd7478e | Paul Kelly | # +html_options+ : Hash containing html options for the link or span
|
|
def link_to_if_authorized(name, options = {}, html_options = {})
|
|||
auth_action = options.delete :auth_action
|
|||
05ab4c16 | Ohad Levy | enable_link = authorized_for(options[:controller] || params[:controller], auth_action || options[:action])
|
|
9fd7478e | Paul Kelly | if enable_link
|
|
05ab4c16 | Ohad Levy | link_to name, options, html_options
|
|
9fd7478e | Paul Kelly | else
|
|
a70ad048 | Ohad Levy | link_to_function name, nil, html_options.merge!(:class => "#{html_options[:class]} disabled", :disabled => true)
|
|
9fd7478e | Paul Kelly | end
|
|
end
|
|||
82e47ae0 | Amos Benari | def display_delete_if_authorized(options ={}, html_options ={})
|
|
options = {:auth_action => :destroy}.merge(options)
|
|||
html_options = {:confirm => 'Are you sure?', :method => :delete, :class => 'delete'}.merge(html_options)
|
|||
display_link_if_authorized("Delete", options, html_options)
|
|||
end
|
|||
9fd7478e | Paul Kelly | # Display a link if user is authorized, otherwise nothing
|
|
# +name+ : String to be displayed
|
|||
# +options+ : Hash containing
|
|||
572a19c3 | Paul Kelly | # :controller : String or Symbol representing the controller
|
|
# :auth_action : String or Symbol representing the action to be used for authorization checks
|
|||
9fd7478e | Paul Kelly | # +html_options+ : Hash containing html options for the link or span
|
|
c45a0014 | Ohad Levy | def display_link_if_authorized(name, options = {}, html_options = {})
|
|
9fd7478e | Paul Kelly | auth_action = options.delete :auth_action
|
|
c45a0014 | Ohad Levy | enable_link = html_options.has_key?(:disabled) ? !html_options[:disabled] : true
|
|
if enable_link and authorized_for(options[:controller] || params[:controller], auth_action || options[:action])
|
|||
01984fb7 | Amos Benari | link_to(name, options, html_options)
|
|
else
|
|||
""
|
|||
end
|
|||
9fd7478e | Paul Kelly | end
|
|
def authorized_edit_habtm klass, association
|
|||
return edit_habtm(klass, association) if authorized_for params[:controller], params[:action]
|
|||
show_habtm klass.send(association.name.pluralize.downcase)
|
|||
end
|
|||
cab0d8c6 | Ohad Levy | # renders a style=display based on an attribute properties
|
|
69f9cb82 | Ohad Levy | def display? attribute = true
|
|
36f93e4d | Ohad Levy | "style=#{display(attribute)}"
|
|
end
|
|||
def display attribute
|
|||
"display:#{attribute ? 'none' : 'inline'};"
|
|||
cab0d8c6 | Ohad Levy | end
|
|
743a5d9c | Ohad Levy | # return our current model instance type based on the current controller
|
|
# i.e. HostsController would return "host"
|
|||
def type
|
|||
controller_name.singularize
|
|||
end
|
|||
cdf02336 | Ohad Levy | def checked_icon condition
|
|
67799065 | Ohad Levy | image_tag("toggle_check.png") if condition
|
|
cdf02336 | Ohad Levy | end
|
|
80516e41 | Ohad Levy | def searchable?
|
|
8003e4f9 | Amos Benari | return false if (SETTINGS[:login] and !User.current )
|
|
67799065 | Ohad Levy | return false unless @searchbar
|
|
17637a75 | Ohad Levy | if (controller.action_name == "index") or (defined?(SEARCHABLE_ACTIONS) and (SEARCHABLE_ACTIONS.include?(controller.action_name)))
|
|
controller.respond_to?(:auto_complete_search)
|
|||
end
|
|||
80516e41 | Ohad Levy | end
|
|
def auto_complete_search(method, val,tag_options = {}, completion_options = {})
|
|||
path = eval("#{controller_name}_path")
|
|||
options = tag_options.merge(:class => "auto_complete_input")
|
|||
text_field_tag(method, val, options) + auto_complete_clear_value_button(method) +
|
|||
auto_complete_field_jquery(method, "#{path}/auto_complete_#{method}", completion_options)
|
|||
end
|
|||
05ab4c16 | Ohad Levy | def help_path
|
|
017e1049 | Ohad Levy | link_to "Help", :action => "welcome"
|
|
05ab4c16 | Ohad Levy | end
|
|
52e114a6 | Ohad Levy | def edit_textfield(object, property, options={})
|
|
edit_inline(object, property, options.merge({:type => "edit_textfield"}))
|
|||
end
|
|||
def edit_textarea(object, property, options={})
|
|||
edit_inline(object, property, options.merge({:type => "edit_textarea"}))
|
|||
end
|
|||
5d8d8a6e | Ohad Levy | def edit_select(object, property, options={})
|
|
edit_inline(object, property, options.merge({:type => "edit_select"}))
|
|||
end
|
|||
52e114a6 | Ohad Levy | ||
48c5cd3e | Amos Benari | def pie_chart name, title, data, options = {}
|
|
fb45bb29 | Amos Benari | link_to_function(content_tag(:h4,title,:class=>'ca'), "expand_chart(this)") +
|
|
48c5cd3e | Amos Benari | content_tag(:div, nil,
|
|
9442eac2 | Amos Benari | { :id => name,
|
|
:class => 'statistics_pie',
|
|||
:'chart-name' => name,
|
|||
:'chart-title' => title,
|
|||
a7f7ee02 | Amos Benari | :'chart-data' => data.to_a.to_json,
|
|
5a6d2ffc | Amos Benari | :'chart-href' => options[:search] ? "/hosts?search=#{URI.encode(options.delete(:search))}" : "#"
|
|
9442eac2 | Amos Benari | }.merge(options))
|
|
48c5cd3e | Amos Benari | end
|
|
3aea8c81 | Amos Benari | def bar_chart name, title, subtitle, labels, data, options = {}
|
|
48c5cd3e | Amos Benari | content_tag(:div, nil,
|
|
9442eac2 | Amos Benari | { :id => name,
|
|
:class => 'statistics_bar',
|
|||
:'chart-name' => name,
|
|||
:'chart-title' => title,
|
|||
3aea8c81 | Amos Benari | :'chart-subtitle' => subtitle,
|
|
9442eac2 | Amos Benari | :'chart-labels' => labels.to_a.to_json,
|
|
:'chart-data' => data.to_a.to_json
|
|||
48c5cd3e | Amos Benari | }.merge(options))
|
|
c255ab34 | Ohad Levy | end
|
|
82e47ae0 | Amos Benari | def action_buttons(*args)
|
|
# the no-buttons code is needed for users with less permissions
|
|||
return unless args
|
|||
args = args.map{|arg| arg unless arg.blank?}.compact
|
|||
return if args.length == 0
|
|||
#single button
|
|||
return content_tag(:span, args[0].html_safe, :class=>'btn') if args.length == 1
|
|||
#multiple buttons
|
|||
primary = args.delete_at(0)
|
|||
content_tag(:div,:class => "btn-group") do
|
|||
primary + link_to(content_tag(:span, '', :class=>'caret'),'#', :class=>'btn dropdown-toggle', :'data-toggle'=>'dropdown') +
|
|||
content_tag(:ul,:class=>"dropdown-menu") do
|
|||
args.map{|option| content_tag(:li,option)}.join(" ").html_safe
|
|||
end
|
|||
end
|
|||
end
|
|||
05ab4c16 | Ohad Levy | private
|
|
52e114a6 | Ohad Levy | def edit_inline(object, property, options={})
|
|
name = "#{type}[#{property}]"
|
|||
helper = options[:helper]
|
|||
value = helper.nil? ? object.send(property) : self.send(helper, object)
|
|||
klass = options[:type]
|
|||
update_url = options[:update_url] || url_for(object)
|
|||
df886c98 | Ohad Levy | opts = { :title => "Click to edit", "data-url" => update_url, :class => "editable #{klass}",
|
|
5d8d8a6e | Ohad Levy | :name => name, "data-field" => property, :value => value, :select_values => options[:select_values]}
|
|
52e114a6 | Ohad Levy | ||
content_tag_for :span, object, opts do
|
|||
h(value)
|
|||
end
|
|||
end
|
|||
5563217a | Ohad Levy | end
|