# frozen_string_literal: true
module Casein
module CaseinHelper
def casein_get_footer_string(include_version = false)
if include_version
"Running on #{link_to 'Casein', 'http://www.caseincms.com'} #{casein_get_full_version_string}, an open-source project.".html_safe
else
"Running on #{link_to 'Casein', 'http://www.caseincms.com'}, an open-source project.".html_safe
end
end
def casein_get_version_info
Casein::VERSION_HASH
end
def casein_get_full_version_string
"v#{Casein::VERSION}"
end
def casein_get_short_version_string
version_info = casein_get_version_info
"v#{version_info[:major]}"
end
def casein_generate_page_title
return casein_config_website_name if @casein_page_title.nil?
casein_config_website_name + ' > ' + @casein_page_title
end
def casein_get_access_level_text(level)
case level
when $CASEIN_USER_ACCESS_LEVEL_ADMIN
'Administrator'
when $CASEIN_USER_ACCESS_LEVEL_USER
'User'
else
'Unknown'
end
end
def casein_get_access_level_array
[['Administrator', $CASEIN_USER_ACCESS_LEVEL_ADMIN], ['User', $CASEIN_USER_ACCESS_LEVEL_USER]]
end
def casein_pagination_details(objs)
" ".html_safe if objs.current_page && objs.total_pages > 1
end
def casein_table_cell_link(contents, link, options = {})
if options.key? :casein_truncate
contents = truncate(contents, length: options[:casein_truncate], omission: '...')
end
link_to contents.to_s.html_safe, link, options
end
def casein_table_cell_no_link(contents, options = {})
if options.key? :casein_truncate
contents = truncate(contents, length: options[:casein_truncate], omission: '...')
end
"
#{contents}
".html_safe
end
def casein_span_icon(icon_name)
""
end
def casein_show_icon(icon_name)
"#{casein_span_icon icon_name}
".html_safe
end
def casein_show_row_icon(icon_name)
"#{casein_span_icon icon_name}
".html_safe
end
def casein_format_date(date, format = '%b %d, %Y')
date.strftime(format)
end
def casein_format_time(time, format = '%H:%M')
time.strftime(format)
end
def casein_format_datetime(datetime, format = '%b %d, %Y %H:%M')
datetime.strftime(format)
end
def casein_sort_link(title, column, options = {})
condition = options[:unless] if options.key?(:unless)
icon_to_show_html = "".html_safe
if params[:c].to_s == column.to_s
icon_to_show = params[:d] == 'down' ? 'chevron-up' : 'chevron-down'
icon_to_show_html = "".html_safe
end
sort_dir = params[:d] == 'down' ? 'up' : 'down'
link_to_unless(condition, title, request.parameters.merge(c: column, d: sort_dir)) + icon_to_show_html
end
def casein_yes_no_label(value)
if value
"Yes".html_safe
else
"No".html_safe
end
end
def casein_no_yes_label(value)
if value
"Yes".html_safe
else
"No".html_safe
end
end
# Styled form tag helpers
def casein_text_field(form, obj, attribute, options = {})
casein_form_tag_wrapper(form.text_field(attribute, strip_casein_options(options_hash_with_merged_classes(options, 'form-control'))), form, obj, attribute, options).html_safe
end
def casein_password_field(form, obj, attribute, options = {})
casein_form_tag_wrapper(form.password_field(attribute, strip_casein_options(options_hash_with_merged_classes(options, 'form-control'))), form, obj, attribute, options).html_safe
end
def casein_text_area(form, obj, attribute, options = {})
casein_form_tag_wrapper(form.text_area(attribute, strip_casein_options(options_hash_with_merged_classes(options, 'form-control'))), form, obj, attribute, options).html_safe
end
def casein_text_area_big(form, obj, attribute, options = {})
casein_form_tag_wrapper(form.text_area(attribute, strip_casein_options(options_hash_with_merged_classes(options, 'form-control'))), form, obj, attribute, options).html_safe
end
def casein_check_box(form, obj, attribute, options = {})
form_tag = "#{form.check_box(attribute, strip_casein_options(options))}
".html_safe
casein_form_tag_wrapper(form_tag, form, obj, attribute, options).html_safe
end
def casein_check_box_group(form, obj, check_boxes = {})
form_tags = ''
check_boxes.each do |check_box|
form_tags += casein_check_box form, obj, check_box[0], check_box[1]
end
casein_form_tag_wrapper(form_tag, form, obj, attribute, options)
end
def casein_radio_button(form, obj, attribute, tag_value, options = {})
form_tag = form.radio_button(obj, attribute, tag_value, strip_casein_options(options))
if options.key? :casein_button_label
form_tag = '' + form_tag + "#{options[:casein_button_label]}
".html_safe
end
casein_form_tag_wrapper(form_tag, form, obj, attribute, options).html_safe
end
def casein_radio_button_group(form, obj, radio_buttons = {})
form_tags = ''
radio_buttons.each do |_radio_button|
form_tags += casein_radio_button form, obj, check_box[0], check_box[1], check_box[2]
end
casein_form_tag_wrapper(form_tag, form, obj, attribute, options).html_safe
end
def casein_select(form, obj, attribute, option_tags, options = {}, html_options = {})
html_options_to_use = merged_class_hash(options, 'form-control') # legacy support
html_options_to_use = options_hash_with_merged_classes(html_options, html_options_to_use[:class])
casein_form_tag_wrapper(form.select(attribute, option_tags, strip_casein_options(options), html_options_to_use), form, obj, attribute, options).html_safe
end
def casein_time_zone_select(form, obj, attribute, option_tags, options = {})
casein_form_tag_wrapper(form.time_zone_select(attribute, option_tags, strip_casein_options(options), merged_class_hash(options, 'form-control')), form, obj, attribute, options).html_safe
end
# e.g. casein_collection_select f, f.object, :article, :author_id, Author.all, :id, :name, { prompt: 'Select author' }
def casein_collection_select(form, obj, object_name, attribute, collection, value_method, text_method, options = {})
casein_form_tag_wrapper(collection_select(object_name, attribute, collection, value_method, text_method, strip_casein_options(options), merged_class_hash(options, 'form-control')), form, obj, attribute, options).html_safe
end
def casein_date_select(form, obj, attribute, options = {})
casein_form_tag_wrapper("".html_safe + form.date_select(attribute, strip_casein_options(options), merged_class_hash(options, 'form-control')) + '
'.html_safe, form, obj, attribute, options).html_safe
end
def casein_time_select(form, obj, attribute, options = {})
casein_form_tag_wrapper("".html_safe + form.time_select(attribute, strip_casein_options(options), merged_class_hash(options, 'form-control')) + '
'.html_safe, form, obj, attribute, options).html_safe
end
def casein_datetime_select(form, obj, attribute, options = {})
casein_form_tag_wrapper("".html_safe + form.datetime_select(attribute, strip_casein_options(options), merged_class_hash(options, 'form-control')) + '
'.html_safe, form, obj, attribute, options).html_safe
end
def casein_file_field(form, obj, _object_name, attribute, options = {})
class_hash = merged_class_hash(options, 'form-control')
contents = "" + form.file_field(attribute, strip_casein_options(options)) + '
'
if options.key? :casein_contents_preview
contents = options[:casein_contents_preview].html_safe + contents.html_safe
end
casein_form_tag_wrapper(contents, form, obj, attribute, options).html_safe
end
def casein_hidden_field(form, _obj, attribute, options = {})
form.hidden_field(attribute, strip_casein_options(options)).html_safe
end
def casein_color_field(form, obj, attribute, options = {})
casein_form_tag_wrapper(form.color_field(attribute, strip_casein_options(options_hash_with_merged_classes(options, 'form-control'))), form, obj, attribute, options).html_safe
end
def casein_search_field(form, obj, attribute, options = {})
casein_form_tag_wrapper(form.search_field(attribute, strip_casein_options(options_hash_with_merged_classes(options, 'form-control'))), form, obj, attribute, options).html_safe
end
def casein_telephone_field(form, obj, attribute, options = {})
casein_form_tag_wrapper(form.telephone_field(attribute, strip_casein_options(options_hash_with_merged_classes(options, 'form-control'))), form, obj, attribute, options).html_safe
end
def casein_url_field(form, obj, attribute, options = {})
casein_form_tag_wrapper(form.url_field(attribute, strip_casein_options(options_hash_with_merged_classes(options, 'form-control'))), form, obj, attribute, options).html_safe
end
def casein_email_field(form, obj, attribute, options = {})
casein_form_tag_wrapper(form.email_field(attribute, strip_casein_options(options_hash_with_merged_classes(options, 'form-control'))), form, obj, attribute, options).html_safe
end
def casein_date_field(form, obj, attribute, options = {})
casein_form_tag_wrapper(form.date_field(attribute, strip_casein_options(options_hash_with_merged_classes(options, 'form-control'))), form, obj, attribute, options).html_safe
end
def casein_datetime_field(form, obj, attribute, options = {})
casein_form_tag_wrapper(form.datetime_field(attribute, strip_casein_options(options_hash_with_merged_classes(options, 'form-control'))), form, obj, attribute, options).html_safe
end
def casein_datetime_local_field(form, obj, attribute, options = {})
casein_form_tag_wrapper(form.datetime_local_field(attribute, strip_casein_options(options_hash_with_merged_classes(options, 'form-control'))), form, obj, attribute, options).html_safe
end
def casein_month_field(form, obj, attribute, options = {})
casein_form_tag_wrapper(form.month_field(attribute, strip_casein_options(options_hash_with_merged_classes(options, 'form-control'))), form, obj, attribute, options).html_safe
end
def casein_week_field(form, obj, attribute, options = {})
casein_form_tag_wrapper(form.week_field(attribute, strip_casein_options(options_hash_with_merged_classes(options, 'form-control'))), form, obj, attribute, options).html_safe
end
def casein_time_field(form, obj, attribute, options = {})
casein_form_tag_wrapper(form.time_field(attribute, strip_casein_options(options_hash_with_merged_classes(options, 'form-control'))), form, obj, attribute, options).html_safe
end
def casein_number_field(form, obj, attribute, options = {})
casein_form_tag_wrapper(form.number_field(attribute, strip_casein_options(options_hash_with_merged_classes(options, 'form-control'))), form, obj, attribute, options).html_safe
end
def casein_range_field(form, obj, attribute, options = {})
casein_form_tag_wrapper(form.range_field(attribute, strip_casein_options(options_hash_with_merged_classes(options, 'form-control'))), form, obj, attribute, options).html_safe
end
def casein_custom_field(form, obj, attribute, custom_contents, options = {})
casein_form_tag_wrapper(custom_contents, form, obj, attribute, options).html_safe
end
protected
def strip_casein_options(options)
options.reject { |key, _value| key.to_s.include? 'casein_' }
end
def merged_class_hash(options, new_class)
new_class += " #{options[:class]}" if options.key? :class
{ class: new_class }
end
def options_hash_with_merged_classes(options, new_class)
new_class += " #{options[:class]}" if options.key? :class
options[:class] = new_class
options
end
def casein_form_tag_wrapper(form_tag, form, obj, attribute, options = {})
human_attribute_name = if options.key? :casein_label
options[:casein_label]
else
attribute.to_s.humanize.titleize
end
sublabel = ''
if options.key? :casein_sublabel
sublabel = " #{options[:casein_sublabel]}".html_safe
end
html = ''
if obj && obj.errors[attribute].any?
html += "