Sha256: eeec0a50f2917bc70115bc570a2c6204b380ed1a23d9dc014a0f21745c35c2d1

Contents?: true

Size: 1.23 KB

Versions: 4

Compression:

Stored size: 1.23 KB

Contents

require 'action_view/helpers/tag_helper'

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

      attr_reader :authorise, :model, :options

      def initialize(authorise, model, options)
        @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
        options[:if] || authorise
      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

4 entries across 4 versions & 1 rubygems

Version Path
type_station-0.2.3 lib/type_station/blocks/base.rb
type_station-0.2.2 lib/type_station/blocks/base.rb
type_station-0.2.1 lib/type_station/blocks/base.rb
type_station-0.2.0 lib/type_station/blocks/base.rb