module Undercase module ApplicationHelper Dropdown = Struct.new(:link_text, :placement, :contents) def dropdown(link_text, options = {}, &block) contents = capture(&block) placement = options.fetch(:placement) { "left" } dropdown = Dropdown.new(link_text, placement, contents) render "undercase/popups/dropdown", dropdown: dropdown end def content_with_toolbar(toolbar_links) content = content_tag(:div) do yield end toolbar = content_tag(:div, toolbar_links.join.html_safe, class: 'toolbar') content_and_toolbar = content + toolbar content_tag(:div, content_and_toolbar.html_safe, class: 'content-with-toolbar') end def inline_message(type, content_or_options_with_block = nil, options = nil, &block) raise ArgumentError unless [:notice, :pending, :warning, :severe, :success].include?(type) if block_given? options = content_or_options_with_block else content = content_or_options_with_block end options ||= {} options[:class] = ["inline-#{type}", options[:class]].compact.join(" ") if block_given? content_tag(:div, options, &block) else content_tag(:div, content, options) end end def length_of_time(from_date, current_date = Date.today) time_hash = distance_of_time_in_words_hash(from_date, current_date) time_hash = convert_one_year_to_months(time_hash) time_hash = convert_years_to_half_years(time_hash) ["years", "months", "days"].each do |unit| distance = time_hash[unit] if distance && distance > 0 result = '%{distance} %{unit}' if unit == "months" unit = "mos" result += '.' end unit = unit.singularize if distance <= 1 return (result % { unit: unit, distance: distance }).gsub('.5',' ½') end end "1 #{I18n.t(:days, default: "days").singularize}" end private def convert_one_year_to_months(time_hash) return time_hash unless time_hash["years"] == 1 result = time_hash.dup months = result["months"] || 0 result["months"] = months + 12 result["years"] = 0 result end def convert_years_to_half_years(time_hash) return time_hash if time_hash['years'] && time_hash['years'] < 2 result = time_hash.dup months = result['months'] || 0 if months >= 6 result['years'] = result['years'] + 0.5 result['months'] = 0 end result end end end