Module: UtilityHelper
- Included in:
- FormHelperTest, FormatHelperTest, TableHelperTest
- Defined in:
- app/helpers/utility_helper.rb
Overview
View helpers for basic functions used in various other helpers.
Constant Summary collapse
- EMPTY_STRING =
non-breaking space asserts better css.
" ".html_safe.freeze
Instance Method Summary collapse
-
#add_css_class(options, classes) ⇒ Object
Adds a class to the given options, even if there are already classes.
-
#assoc_and_id_attr(attr) ⇒ Object
Returns the name of the attr and it’s corresponding field.
-
#association(obj, attr, *macros) ⇒ Object
Returns the association proxy for the given attribute.
-
#column_property(obj, attr, property) ⇒ Object
Returns an ActiveRecord column property for the passed attr or nil.
-
#column_type(obj, attr) ⇒ Object
Returns the ActiveRecord column type or nil.
-
#content_tag_nested(tag, collection, **options, &block) ⇒ Object
Render a content tag with the collected contents rendered by &block for each item in collection.
-
#default_crud_attrs ⇒ Object
The default attributes to use in attrs, list and form partials.
-
#flash_class(level) ⇒ Object
Returns the css class for the given flash level.
-
#safe_join(array, sep = $OUTPUT_FIELD_SEPARATOR, &block) ⇒ Object
Overridden method that takes a block that is executed for each item in array before appending the results.
Instance Method Details
#add_css_class(options, classes) ⇒ Object
Adds a class to the given options, even if there are already classes.
30 31 32 33 34 35 36 |
# File 'app/helpers/utility_helper.rb', line 30 def add_css_class(, classes) if [:class] [:class] += " #{classes}" if classes else [:class] = classes end end |
#assoc_and_id_attr(attr) ⇒ Object
Returns the name of the attr and it’s corresponding field
72 73 74 75 76 77 78 79 80 81 |
# File 'app/helpers/utility_helper.rb', line 72 def assoc_and_id_attr(attr) attr = attr.to_s if attr.end_with?("_id") [ attr[0..-4], attr ] elsif attr.end_with?("_ids") [ attr[0..-5].pluralize, attr ] else [ attr, "#{attr}_id" ] end end |
#association(obj, attr, *macros) ⇒ Object
Returns the association proxy for the given attribute. The attr parameter may be the _id column or the association name. If a macro (e.g. :belongs_to) is given, the association must be of this type, otherwise, any association is returned. Returns nil if no association (or not of the given macro) was found.
63 64 65 66 67 68 69 |
# File 'app/helpers/utility_helper.rb', line 63 def association(obj, attr, *macros) if obj.class.respond_to?(:reflect_on_association) name = assoc_and_id_attr(attr).first.to_sym assoc = obj.class.reflect_on_association(name) assoc if assoc && (macros.blank? || macros.include?(assoc.macro)) end end |
#column_property(obj, attr, property) ⇒ Object
Returns an ActiveRecord column property for the passed attr or nil
52 53 54 55 56 |
# File 'app/helpers/utility_helper.rb', line 52 def column_property(obj, attr, property) if obj.respond_to?(:column_for_attribute) && obj.has_attribute?(attr) obj.column_for_attribute(attr).send(property) end end |
#column_type(obj, attr) ⇒ Object
Returns the ActiveRecord column type or nil.
47 48 49 |
# File 'app/helpers/utility_helper.rb', line 47 def column_type(obj, attr) column_property(obj, attr, :type) end |
#content_tag_nested(tag, collection, **options, &block) ⇒ Object
Render a content tag with the collected contents rendered by &block for each item in collection.
10 11 12 |
# File 'app/helpers/utility_helper.rb', line 10 def content_tag_nested(tag, collection, **, &block) content_tag(tag, safe_join(collection, &block), **) end |
#default_crud_attrs ⇒ Object
The default attributes to use in attrs, list and form partials. These are all defined attributes except certain special ones like ‘id’ or ‘position’.
41 42 43 44 |
# File 'app/helpers/utility_helper.rb', line 41 def default_crud_attrs attrs = model_class.column_names.map(&:to_sym) attrs - i[id position password] end |
#flash_class(level) ⇒ Object
Returns the css class for the given flash level.
21 22 23 24 25 26 27 |
# File 'app/helpers/utility_helper.rb', line 21 def flash_class(level) case level when :notice then "success" when :alert then "error" else level.to_s end end |
#safe_join(array, sep = $OUTPUT_FIELD_SEPARATOR, &block) ⇒ Object
Overridden method that takes a block that is executed for each item in array before appending the results.
16 17 18 |
# File 'app/helpers/utility_helper.rb', line 16 def safe_join(array, sep = $OUTPUT_FIELD_SEPARATOR, &block) super(block_given? ? array.map(&block).compact : array, sep) end |