interage-helper 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +4 -0
  4. data/.rubocop.yml +22 -0
  5. data/.travis.yml +7 -0
  6. data/Gemfile +22 -0
  7. data/Gemfile.lock +80 -0
  8. data/README.md +40 -0
  9. data/Rakefile +8 -0
  10. data/bin/brakeman +29 -0
  11. data/bin/bundle +105 -0
  12. data/bin/ci +12 -0
  13. data/bin/console +14 -0
  14. data/bin/rspec +29 -0
  15. data/bin/rubocop +29 -0
  16. data/bin/setup +8 -0
  17. data/interage-helper.gemspec +43 -0
  18. data/lib/generators/interage/helper/install/USAGE +8 -0
  19. data/lib/generators/interage/helper/install/install_generator.rb +26 -0
  20. data/lib/generators/interage/helper/install/templates/application_helper.rb +5 -0
  21. data/lib/generators/interage/helper/install/templates/application_helper.yml +95 -0
  22. data/lib/interage/application_helper.rb +55 -0
  23. data/lib/interage/application_icon_helper.rb +31 -0
  24. data/lib/interage/bootstrap_helper.rb +53 -0
  25. data/lib/interage/cep_helper.rb +11 -0
  26. data/lib/interage/cocoon_helper.rb +31 -0
  27. data/lib/interage/controller_active_helper.rb +17 -0
  28. data/lib/interage/cpf_cnpj_helper.rb +23 -0
  29. data/lib/interage/date_time_helper.rb +27 -0
  30. data/lib/interage/favicon_helper.rb +31 -0
  31. data/lib/interage/flash_message_helper.rb +21 -0
  32. data/lib/interage/font_awesome_helper.rb +30 -0
  33. data/lib/interage/gravatar_helper.rb +13 -0
  34. data/lib/interage/helper/version.rb +7 -0
  35. data/lib/interage/helper.rb +32 -0
  36. data/lib/interage/link_to_helper.rb +60 -0
  37. data/lib/interage/material_design_icons_helper.rb +25 -0
  38. data/lib/interage/number_helper.rb +9 -0
  39. data/lib/interage/pagination_helper.rb +11 -0
  40. data/lib/interage/phone_helper.rb +13 -0
  41. data/lib/interage/text_helper.rb +13 -0
  42. data/lib/interage/translation_helper.rb +127 -0
  43. data/lib/interage/version_helper.rb +21 -0
  44. metadata +131 -0
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Interage
4
+ module ApplicationIconHelper
5
+ DEFAULT_FONT_ICON = ENV.fetch('DEFAULT_FONT_ICON', 'fa')
6
+
7
+ def app_icon(icon, options = {})
8
+ case DEFAULT_FONT_ICON
9
+ when 'fa'
10
+ fa_icon(icon, options)
11
+ when 'md'
12
+ md_icon(icon, options)
13
+ end
14
+ end
15
+
16
+ def app_icon_text(icon, text, options = {})
17
+ case DEFAULT_FONT_ICON
18
+ when 'fa'
19
+ fa_icon_text(icon, text, options)
20
+ when 'md'
21
+ md_icon_text(icon, text, options)
22
+ end
23
+ end
24
+
25
+ def app_icon_classes(icon, prefix, separator, options = {})
26
+ icon_classes = icon.to_s.split(' ').uniq.join(" #{separator}-")
27
+
28
+ "#{prefix} #{separator}-#{icon_classes} #{options[:class]}".strip
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Interage
4
+ module BootstrapHelper
5
+ def bootstrap_alert(type, message)
6
+ content_tag(:div, message, class: "no-margin alert alert-#{type}")
7
+ end
8
+
9
+ def bootstrap_alert_default(message)
10
+ bootstrap_alert(:default, message)
11
+ end
12
+
13
+ def bootstrap_alert_info(message)
14
+ bootstrap_alert(:info, message)
15
+ end
16
+
17
+ def bootstrap_alert_warning(message)
18
+ bootstrap_alert(:warning, message)
19
+ end
20
+
21
+ def bootstrap_alert_success(message)
22
+ bootstrap_alert(:success, message)
23
+ end
24
+
25
+ def bootstrap_alert_danger(message)
26
+ bootstrap_alert(:danger, message)
27
+ end
28
+
29
+ def bootstrap_alert_not_found(gender, model)
30
+ bootstrap_alert_info(text_not_found(gender, model))
31
+ end
32
+
33
+ def bootstrap_alert_not_found_male(model)
34
+ bootstrap_alert_not_found(:male, model)
35
+ end
36
+
37
+ def bootstrap_alert_not_found_female(model)
38
+ bootstrap_alert_not_found(:female, model)
39
+ end
40
+
41
+ def text_not_found(gender, model)
42
+ t("index.model.#{gender}.not_found", model: tm(model).downcase)
43
+ end
44
+
45
+ def text_not_found_male(model)
46
+ text_not_found(:male, model)
47
+ end
48
+
49
+ def text_not_found_female(model)
50
+ text_not_found(:female, model)
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Interage
4
+ module CEPHelper
5
+ def format_cep(cep)
6
+ return if cep.blank?
7
+
8
+ "#{cep[0, 5]}-#{cep[5, 3]}".strip
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Interage
4
+ module CocoonHelper
5
+ DEFAULT_BTN_CLASS = 'btn btn-sm btn-outline-'
6
+
7
+ def cocoon_link_to_add_association(form, association, options = {})
8
+ label = options[:label] || t('buttons.cocoon.add.text')
9
+ html_class = options[:html_class] || cocoon_default_btn_class('success')
10
+
11
+ link_to_add_association(form, association, class: html_class) do
12
+ app_icon_text(t('buttons.cocoon.add.icon'), label)
13
+ end
14
+ end
15
+
16
+ def cocoon_link_to_remove_association(form, label = nil)
17
+ label ||= t('buttons.cocoon.remove.text')
18
+ html_class = cocoon_default_btn_class('danger')
19
+
20
+ content_tag :div, class: 'cocoon-link-to-remove' do
21
+ link_to_remove_association(form, class: html_class, title: label) do
22
+ app_icon_text(t('buttons.cocoon.remove.icon'), label)
23
+ end
24
+ end
25
+ end
26
+
27
+ def cocoon_default_btn_class(type)
28
+ "#{DEFAULT_BTN_CLASS}#{type} text-truncate"
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Interage
4
+ module ControllerActiveHelper
5
+ HTML_ACTIVE_CLASS = 'active'
6
+
7
+ def menu_active(*controllers)
8
+ HTML_ACTIVE_CLASS if current_controller?(*controllers)
9
+ end
10
+
11
+ def current_controller?(*controllers)
12
+ controllers = controllers.is_a?(Array) ? controllers : [controllers]
13
+
14
+ controllers.include?(params[:controller])
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Interage
4
+ module CPFCNPJHelper
5
+ def format_cnpj(cnpj)
6
+ return if cnpj.blank?
7
+
8
+ cnpj.to_s.gsub!(/[^0-9]/, '')
9
+
10
+ formatted_cnpj = ["#{cnpj[0, 2]}.#{cnpj[2, 3]}.#{cnpj[5, 3]}",
11
+ "#{cnpj[8, 4]}-#{cnpj[12, 2]}"]
12
+ formatted_cnpj.join('/').strip
13
+ end
14
+
15
+ def format_cpf(cpf)
16
+ return if cpf.blank?
17
+
18
+ cpf.to_s.gsub!(/[^0-9]/, '')
19
+
20
+ "#{cpf[0, 3]}.#{cpf[3, 3]}.#{cpf[6, 3]}-#{cpf[9, 2]}".strip
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Interage
4
+ module DateTimeHelper
5
+ def business_days_from_now(days = 2)
6
+ date = days.to_i.business_days.from_now.to_date
7
+
8
+ return l(date) if business_day?(date)
9
+
10
+ business_days_from_now(days + 1)
11
+ end
12
+
13
+ def business_day?(date)
14
+ Holidays.on(date, :br).empty? && date.to_date.workday?
15
+ end
16
+
17
+ def format_date(date, date_format = :date_time)
18
+ l(date, format: date_format)
19
+ end
20
+
21
+ def format_time(time)
22
+ return if time.blank?
23
+
24
+ time.strftime('%H:%M')
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Interage
4
+ module FaviconHelper
5
+ EXTENTION = 'png'
6
+ FILE_NAME = 'icons/apple-touch-icon'
7
+ FAVICON_SIZES = [nil, 57, 72, 76, 114, 120, 144, 152, 180].freeze
8
+
9
+ def favicon_link_tags
10
+ safe_join favicon_size_link_tags.push(favicon_link_tag)
11
+ end
12
+
13
+ def favicon_size_link_tags
14
+ FAVICON_SIZES.map do |s|
15
+ favicon_link_tag(handler_name(s), favicon_options(s))
16
+ end
17
+ end
18
+
19
+ def handler_name(size)
20
+ "#{FILE_NAME}#{handler_size(size, '-')}.#{EXTENTION}"
21
+ end
22
+
23
+ def favicon_options(size)
24
+ { type: 'image/png', rel: 'apple-touch-icon', sizes: handler_size(size) }
25
+ end
26
+
27
+ def handler_size(size, prefix = '')
28
+ "#{prefix}#{size}x#{size}" if size.present?
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Interage
4
+ module FlashMessageHelper
5
+ def flash_messages
6
+ messages ||= flashes.map do |type, message|
7
+ bootstrap_alert(handle_type(type), message)
8
+ end
9
+
10
+ safe_join(messages || [])
11
+ end
12
+
13
+ def flashes
14
+ flash.to_h.symbolize_keys
15
+ end
16
+
17
+ def handle_type(type)
18
+ type == :notice ? :success : type
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Interage
4
+ module FontAwesomeHelper
5
+ def fa_icons
6
+ t('icons_alias', default: {}).keys
7
+ end
8
+
9
+ def fa_icon(icon, options = {})
10
+ content_tag :i, nil, options.merge(class: fa_classes(icon, options))
11
+ end
12
+
13
+ def fa_fw_icon(icon, options = {})
14
+ fa_icon("fw #{icon}", options)
15
+ end
16
+
17
+ def fa_icon_text(icon, text, options = {})
18
+ content_tag :span do
19
+ concat fa_fw_icon(icon, options)
20
+ concat text
21
+ end
22
+ end
23
+
24
+ def fa_classes(icon, options = {})
25
+ icon_classes = icon.to_s.split(' ').uniq.join(' fa-')
26
+
27
+ "fa fa-#{icon_classes} #{options[:class]}".strip
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Interage
4
+ module GravatarHelper
5
+ BASE_URL = 'https://www.gravatar.com/avatar'
6
+
7
+ def gravatar_image_tag(email, size = 50, options = {})
8
+ hexdigest = Digest::MD5.hexdigest(email)
9
+
10
+ image_tag("#{BASE_URL}/#{hexdigest}?size=#{size}", options)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Interage
4
+ module Helper
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'interage/helper/version'
4
+ require 'active_support/core_ext/module'
5
+
6
+ module Interage
7
+ module Helper
8
+ class Error < StandardError
9
+ end
10
+ end
11
+
12
+ autoload :ApplicationHelper, 'interage/application_helper'
13
+ autoload :ApplicationIconHelper, 'interage/application_icon_helper'
14
+ autoload :BootstrapHelper, 'interage/bootstrap_helper'
15
+ autoload :CEPHelper, 'interage/cep_helper'
16
+ autoload :CocoonHelper, 'interage/cocoon_helper'
17
+ autoload :ControllerActiveHelper, 'interage/controller_active_helper'
18
+ autoload :CPFCNPJHelper, 'interage/cpf_cnpj_helper'
19
+ autoload :DateTimeHelper, 'interage/date_time_helper'
20
+ autoload :FaviconHelper, 'interage/favicon_helper'
21
+ autoload :FlashMessageHelper, 'interage/flash_message_helper'
22
+ autoload :FontAwesomeHelper, 'interage/font_awesome_helper'
23
+ autoload :GravatarHelper, 'interage/gravatar_helper'
24
+ autoload :LinkToHelper, 'interage/link_to_helper'
25
+ autoload :MaterialDesignIconsHelper, 'interage/material_design_icons_helper'
26
+ autoload :NumberHelper, 'interage/number_helper'
27
+ autoload :PaginationHelper, 'interage/pagination_helper'
28
+ autoload :PhoneHelper, 'interage/phone_helper'
29
+ autoload :TextHelper, 'interage/text_helper'
30
+ autoload :TranslationHelper, 'interage/translation_helper'
31
+ autoload :VersionHelper, 'interage/version_helper'
32
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Interage
4
+ module LinkToHelper
5
+ ASIDE_DEFAULT_CLASS = 'list-group-item'
6
+ DESTROY_CONFIRM_MESSAGE = 'Tem certeza que deseja apagar?'
7
+ PREFIX_BUTTON_CLASS = 'btn btn-sm btn-outline-'
8
+
9
+ def aside_link_to(text, url = '#', html_options = {})
10
+ html_options[:class] = "#{ASIDE_DEFAULT_CLASS} #{html_options[:class]}"
11
+ html_options[:title] = strip_tags(text)
12
+
13
+ link_to text, url, html_options
14
+ end
15
+
16
+ def link_to_modal(text, url = '#', html_options = {})
17
+ link_to text, url, html_options.merge('data-toggle': 'modal')
18
+ end
19
+
20
+ def link_to_new(resource, url)
21
+ text = t('menu.links.new', model: tm(resource).downcase)
22
+
23
+ link_to url, title: strip_tags(text), class: 'btn btn-outline-primary' do
24
+ app_icon_text(t('menu.icons.new'), text)
25
+ end
26
+ end
27
+
28
+ def link_to_edit(url)
29
+ link_to_default(:edit, url, class: button_class('info'))
30
+ end
31
+
32
+ def link_to_back(url)
33
+ link_to_default(:back, url, class: button_class('secondary'))
34
+ end
35
+
36
+ def link_to_show(url)
37
+ link_to_default(:show, url, class: button_class('dark'))
38
+ end
39
+
40
+ def link_to_destroy(url, html_options = {})
41
+ html_options.merge!(method: :delete,
42
+ 'data-confirm' => DESTROY_CONFIRM_MESSAGE,
43
+ class: button_class('danger', html_options[:class]))
44
+
45
+ link_to_default(:destroy, url, html_options)
46
+ end
47
+
48
+ def link_to_default(type, url, html_options = {})
49
+ text = t("menu.links.#{type}")
50
+ html_options = html_options.merge(title: strip_tags(text),
51
+ data: { tooltip: 'top' })
52
+
53
+ link_to(app_icon_text(t("menu.icons.#{type}"), text), url, html_options)
54
+ end
55
+
56
+ def button_class(type, addicional_class = nil)
57
+ "#{PREFIX_BUTTON_CLASS}#{type} #{addicional_class}"
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Interage
4
+ module MaterialDesignIconsHelper
5
+ def md_icon(icon, options = {})
6
+ icon_classes = icon.to_s.split(' ')
7
+ icon = icon_classes.shift
8
+
9
+ content_tag :i, class: md_classes(icon, options) do
10
+ t("icons_alias.#{icon}", default: icon)
11
+ end
12
+ end
13
+
14
+ def md_icon_text(icon, text, options = {})
15
+ content_tag :span do
16
+ concat md_icon(icon, options)
17
+ concat " #{text}"
18
+ end
19
+ end
20
+
21
+ def md_classes(icon, options = {})
22
+ app_icon_classes(icon, 'material-icons', 'md', options)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Interage
4
+ module NumberHelper
5
+ def default_currency_format(number, precision = 2, unit = 'R$')
6
+ number_to_currency(number, precision: precision, unit: unit)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Interage
4
+ module PaginationHelper
5
+ def pagination_links(collection)
6
+ return if collection.empty?
7
+
8
+ render 'kaminari/pagination', collection: collection
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Interage
4
+ module PhoneHelper
5
+ def format_phone(phone)
6
+ return if phone.blank?
7
+
8
+ phone.to_s.gsub!(/[^0-9]/, '')
9
+
10
+ "(#{phone[0, 2]}) #{phone[3, 5]}-#{phone[8, 4]}".strip
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Interage
4
+ module TextHelper
5
+ def nl2br(string, options = {})
6
+ out = string.split("\n").map do |str|
7
+ content_tag :div, str, options
8
+ end
9
+
10
+ safe_join out
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,127 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Interage
4
+ module TranslationHelper
5
+ ##
6
+ # Public: Translate a model name.
7
+ #
8
+ # model: Model class.
9
+ # count: Count.
10
+ #
11
+ # Examples:
12
+ #
13
+ # <%= tm Post %>
14
+ # # => 'Artigo'
15
+ #
16
+ # <%= tm Post, 2 %>
17
+ # # => 'Artigos'
18
+ #
19
+ # Returns translated model.
20
+ def translate_model_name(model, count = 1)
21
+ model.model_name.human(count: count)
22
+ end
23
+ alias tm translate_model_name
24
+
25
+ ##
26
+ # Public: Translate a model name pluralized.
27
+ #
28
+ # model: Model class.
29
+ #
30
+ # Examples:
31
+ #
32
+ # <%= tm Post %>
33
+ # # => 'Artigos'
34
+ #
35
+ # Returns translated model pluralized.
36
+ def translate_model_name_pluralized(model)
37
+ translate_model_name(model, 2)
38
+ end
39
+ alias tmp translate_model_name_pluralized
40
+
41
+ ##
42
+ # Public: Translate a model attribute.
43
+ #
44
+ # model: Model class.
45
+ # attribute: Attribute name.
46
+ # count: Count.
47
+ #
48
+ # Examples
49
+ #
50
+ # <%= ta Post, :title %>
51
+ # # => 'Titulo'
52
+ #
53
+ # <%= ta Post, :title, 2 %>
54
+ # # => 'Titulos'
55
+ #
56
+ # Returns translated model's attribute.
57
+ def translate_model_attribute(model, attribute, count = 1)
58
+ model.human_attribute_name(attribute, count: count)
59
+ end
60
+ alias ta translate_model_attribute
61
+
62
+ ##
63
+ # Public: Translate a boolean attribute.
64
+ #
65
+ # value: TrueClass or FalseClass.
66
+ #
67
+ # Examples
68
+ #
69
+ # <%= tb true %>
70
+ # # => 'Sim'
71
+ #
72
+ # <%= tb false %>
73
+ # # => 'Nao'
74
+ #
75
+ # Returns translated boolean's attribute.
76
+ def translate_boolean(value)
77
+ value ? t('boolean.truly') : t('boolean.falsely')
78
+ end
79
+ alias tb translate_boolean
80
+
81
+ ##
82
+ # Public: Translate a boolean attribute in an icon.
83
+ #
84
+ # value: TrueClass or FalseClass.
85
+ #
86
+ # Examples
87
+ #
88
+ # <%= tbi true %>
89
+ # # => '<i class="fa fa-check text-success"></i>'
90
+ #
91
+ # <%= tbi false %>
92
+ # # => '<i class="fa fa-times text-danger"></i>'
93
+ #
94
+ # Returns translated boolean's attribute in an icon.
95
+ def translate_boolean_in_icon(value)
96
+ if value
97
+ content_tag(:span, '✓', class: 'text-success')
98
+ else
99
+ content_tag(:span, '×', class: 'text-danger')
100
+ end
101
+ end
102
+ alias tbi translate_boolean_in_icon
103
+
104
+ ##
105
+ # Public: Translate a boolean attribute in an icon.
106
+ #
107
+ # value: TrueClass or FalseClass.
108
+ #
109
+ # Examples
110
+ #
111
+ # <%= tbci true %>
112
+ # # => '<i class="fa fa-check-square-o text-default"></i>'
113
+ #
114
+ # <%= tbci false %>
115
+ # # => '<i class="fa fa-square-o text-default"></i>'
116
+ #
117
+ # Returns translated boolean's attribute in an icon.
118
+ def translate_boolean_in_check_box_icon(value)
119
+ if value
120
+ app_icon('check', class: 'text-success')
121
+ else
122
+ app_icon('times', class: 'text-danger')
123
+ end
124
+ end
125
+ alias tbci translate_boolean_in_check_box_icon
126
+ end
127
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Interage
4
+ module VersionHelper
5
+ def app_version
6
+ return if app_text_version.blank?
7
+
8
+ content_tag :div, app_text_version, id: 'app-version'
9
+ end
10
+
11
+ def app_text_version
12
+ return unless file_version_exist?
13
+
14
+ "#{app_name} - v#{File.read('VERSION').strip}"
15
+ end
16
+
17
+ def file_version_exist?
18
+ File.exist?('VERSION')
19
+ end
20
+ end
21
+ end