Module: Binda::ComponentsHelper

Defined in:
app/helpers/binda/components_helper.rb

Instance Method Summary collapse

Instance Method Details

Get the sort index link

This helper generate the a link to the page where to sort components, or

a broken link that tells you that you have too many components to go to that page.


9
10
11
12
13
14
15
# File 'app/helpers/binda/components_helper.rb', line 9

def get_sort_index_link
  if @structure.components.length < Component.sort_limit
    link_to "<i class=\"fa fa-random\" aria-hidden=\"true\"></i>Sort #{ @structure.name.humanize.split.map(&:capitalize).join(' ').pluralize }".html_safe, structure_components_sort_index_path, class: 'main-header--link b-btn b-btn-primary'
  else
    link_to "Sort #{ @structure.name.humanize.split.map(&:capitalize).join(' ').pluralize }", '#', class: 'main-header--link b-btn b-btn-primary', onclick: "alert(\"Sorry! It's not possible to sort #{@structure.name.pluralize} anymore. You currently have more than #{Component.sort_limit} #{@structure.name.pluralize} which is the maximum limit.\")"
  end
end

Get sort link by argument

This method returns a URL which contains the current sort options

and update just the title one. The URL then is used to change the 
sorting of the index in which is used.

Rails guide reference –> guides.rubyonrails.org/action_controller_overview.html#hash-and-array-parameters

Parameters:

  • argument (string)

    This should be the database column on which sort will be based

Returns:

  • (string)

    The URL with the encoded parameters



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/helpers/binda/components_helper.rb', line 72

def get_sort_link_by argument
  arg = "#{argument}".to_sym
  if params[:order].nil? 
    structure_components_path( [@structure], { order: {arg => "DESC"} } )
  else
    order_hash = params[:order].permit(:name, :publish_state).to_h
    if order_hash[arg] == "ASC"
      order_hash[arg] = "DESC"
      structure_components_path( [@structure], { order: order_hash }  )
    else
      order_hash[arg] = "ASC"
      structure_components_path( [@structure], { order: order_hash }  )
    end
  end
end

Get sort link icon by argument

This method returns a Font Awesome icon

Parameters:

  • arg (string)

    This should be the database column on which sort will be based

Returns:

  • (string)

    The icon which needs to be escaped with the ‘html_safe` method



95
96
97
98
99
100
101
102
103
104
# File 'app/helpers/binda/components_helper.rb', line 95

def get_sort_link_icon_by arg
  case 
  when params[:order].nil?
    '<i class="fas fa-sort-alpha-down"></i>'
  when params[:order][arg] == "DESC"
    '<i class="fas fa-sort-alpha-up"></i>'
  else
    '<i class="fas fa-sort-alpha-down"></i>'
  end
end

#prepare_description_for_form_hint(field_setting) ⇒ Object

Prepare description for form hint.

This helper return the field description (as ‘string`) or false (as `boolean`)

in order to tell Simple Form whether to generate or not the hint html tag.

Parameters:



23
24
25
26
27
28
29
# File 'app/helpers/binda/components_helper.rb', line 23

def prepare_description_for_form_hint(field_setting)
  if field_setting.description.blank?
    return false
  else
    return field_setting.description
  end
end

#prepare_description_for_selections_form_hint(field_setting) ⇒ Object

Prepare description for form hint belonging to select, radio and checkbox fields.

This helper return the field description (as ‘string`) or false (as `boolean`)

in order to tell Simple Form whether to generate or not the hint html tag.

Parameters:



38
39
40
41
42
43
44
# File 'app/helpers/binda/components_helper.rb', line 38

def prepare_description_for_selections_form_hint(field_setting)
  unless field_setting.description.blank? && field_setting.allow_null?
    prepare_description_for_selections_from_hint_with(field_setting)
  else
    return false 
  end
end

#prepare_description_for_selections_from_hint_with(field_setting) ⇒ String

Prepare description for selections with field setting detail

Parameters:

  • field_setting

    The field setting

Returns:

  • (String)

    The description



53
54
55
56
57
58
# File 'app/helpers/binda/components_helper.rb', line 53

def prepare_description_for_selections_from_hint_with(field_setting)
  description = []
  description << field_setting.description unless field_setting.description.blank?
    description << I18n.t("binda.null_is_not_allowed") if !field_setting.allow_null? 
    return description.join('. ')
end