Sha256: 2bc8fa8cb30b9ec2fbfdb8118934c3c7850719d8a042526cde2a5c47affc1b75

Contents?: true

Size: 1.38 KB

Versions: 12

Compression:

Stored size: 1.38 KB

Contents

require 'action_view/helpers/tag_helper'
require "rails/html/sanitizer"

module TypeStation
  module Blocks
    class Base
      include ActionView::Helpers::TagHelper

      attr_reader :authorise, :model, :options

      def initialize(authorise, model, options)
        @sanitizer = Rails::Html::WhiteListSanitizer.new
        @authorise = authorise
        @model = model
        @options = options
      end

      def render(content)
        if showifblock
          render_edit(content)
        else
          render_default(content)
        end
      end

      def data
        {}
      end

      private

      def render_edit(content)
        content_tag(tag_name, content.html_safe, class: tag_class, id: tag_id, data: tag_data)
      end

      def render_default(content)
        if options[:stick].present?
          content_tag(tag_name, content.html_safe, class: tag_class, id: tag_id)
        else
          content.html_safe
        end
      end

      def showifblock
        if options.has_key? :if
          options[:if]
        else
          authorise
        end
      end

      def tag_id
        options[:id] || "#{model.to_param}-#{SecureRandom.hex(3)}"
      end

      def tag_name
        options[:content_tag] || :div
      end

      def tag_class
        options[:class]
      end

      def tag_data
        { ts: data.merge({url: options[:url] }) }
      end

    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
type_station-0.7.0 lib/type_station/blocks/base.rb
type_station-0.6.0 lib/type_station/blocks/base.rb
type_station-0.5.4 lib/type_station/blocks/base.rb
type_station-0.5.3 lib/type_station/blocks/base.rb
type_station-0.5.2 lib/type_station/blocks/base.rb
type_station-0.5.1 lib/type_station/blocks/base.rb
type_station-0.4.7 lib/type_station/blocks/base.rb
type_station-0.4.6 lib/type_station/blocks/base.rb
type_station-0.4.5 lib/type_station/blocks/base.rb
type_station-0.4.4 lib/type_station/blocks/base.rb
type_station-0.4.3 lib/type_station/blocks/base.rb
type_station-0.4.2 lib/type_station/blocks/base.rb