Sha256: 5c75fc09ccd82995c4efda76604bcb3b50afa2fc750ff91b889facb44f524a3c

Contents?: true

Size: 1.61 KB

Versions: 6

Compression:

Stored size: 1.61 KB

Contents

module Cmor
  module Carousels
    # Usage:
    #
    #     # app/controllers/application_controller.rb
    #     class ApplicationController < ActionController::Base
    #       view_helper Cmor::Carousels::ApplicationViewHelper, as: :carousels_helper
    #     end
    #
    class ApplicationViewHelper < Rao::ViewHelper::Base
      # Example:
      #
      #     # This will render a bootstrap 4 compatible carousel
      #     = carousels_helper(self).render(:main, autostart: true, interval: 5.0, variant_options: { resize: "1920x1080" })
      #
      def render(identifier, options = {})
        options.reverse_merge!(autostart: true, interval: 5.0, keyboard: true, pause: :hover, ride: false, wrap: true, controls: true)
        carousel_options = options.slice(:autostart, :interval, :pause, :ride, :wrap)
        data_attributes = carousel_options_to_data_attributes(carousel_options)

        carousel = Cmor::Carousels::Carousel.where(identifier: identifier.to_s).for_locale(I18n.locale).first

        if carousel.nil?
          return I18n.t('cmor.carousels.carousel.warnings.not_found', identifier: identifier)
        else
          variant_options = options.delete(:variant_options) || carousel.variant_options
          return c.render partial: 'cmor/carousels/application_view_helper/render', locals: { carousel: carousel, options: options, data_attributes: data_attributes, variant_options: variant_options }
        end
      end

      private

      def carousel_options_to_data_attributes(carousel_options)
        carousel_options.collect { |k,v| "\"data-#{k.to_s.dasherize}\"=#{v}" }.join(" ")
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
cmor_carousels-0.0.6.pre app/view_helpers/cmor/carousels/application_view_helper.rb
cmor_carousels-0.0.5.pre app/view_helpers/cmor/carousels/application_view_helper.rb
cmor_carousels-0.0.4.pre app/view_helpers/cmor/carousels/application_view_helper.rb
cmor_carousels-0.0.3.pre app/view_helpers/cmor/carousels/application_view_helper.rb
cmor_carousels-0.0.2.pre app/view_helpers/cmor/carousels/application_view_helper.rb
cmor_carousels-0.0.1.pre app/view_helpers/cmor/carousels/application_view_helper.rb