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.
31 32 33 34 35 36 37 |
# File 'app/helpers/utility_helper.rb', line 31 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
73 74 75 76 77 78 79 80 81 82 |
# File 'app/helpers/utility_helper.rb', line 73 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.
64 65 66 67 68 69 70 |
# File 'app/helpers/utility_helper.rb', line 64 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
53 54 55 56 57 |
# File 'app/helpers/utility_helper.rb', line 53 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.
48 49 50 |
# File 'app/helpers/utility_helper.rb', line 48 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.
11 12 13 |
# File 'app/helpers/utility_helper.rb', line 11 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’.
42 43 44 45 |
# File 'app/helpers/utility_helper.rb', line 42 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.
22 23 24 25 26 27 28 |
# File 'app/helpers/utility_helper.rb', line 22 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.
17 18 19 |
# File 'app/helpers/utility_helper.rb', line 17 def safe_join(array, sep = $OUTPUT_FIELD_SEPARATOR, &block) super(block_given? ? array.map(&block).compact : array, sep) end |