Sha256: e9d0098c24e672371480c0b029bc80199c93318c2134e257a727aa0ecf511164

Contents?: true

Size: 1.32 KB

Versions: 22

Compression:

Stored size: 1.32 KB

Contents

module TypeStation
  class DSL

    def self.build(name, options = {}, &block)
      Page.new(name, options).call(&block)
    end

    class Page
      attr_reader :model
      def initialize(title, options = {}, parent = nil)
        @title = title
        @options = options
        @parent = parent
        @model = _build_model(@title, @options, @parent)
      end

      def call(&block)
        if block_given?
          if block.arity == 1
            block.call model
          else
            instance_eval &block
          end
        end
        model
      end

      def page(name, options = {}, &block)
        Page.new(name, options, model).call(&block)
      end

      private

      def _build_model(title, options, parent)
        parent_id = parent ? parent.id : nil
        name = options[:name]
        template_name = options[:template] || (parent ? title.parameterize('_') : 'index')
        redirect_to = options[:redirect_to]
        slug = options[:slug]
        status = options[:status] || ::TypeStation::Page::STATUS.last

        model = ::TypeStation::Page.create(title: title, name: name, template_name: template_name, status: status, redirect_to: redirect_to, parent_id: parent_id)

        if slug.present?
          model.slug = slug
          model.save
        end

        model
      end

    end

  end


end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
type_station-0.6.0 lib/type_station/dsl.rb
type_station-0.5.4 lib/type_station/dsl.rb
type_station-0.5.3 lib/type_station/dsl.rb
type_station-0.5.2 lib/type_station/dsl.rb
type_station-0.5.1 lib/type_station/dsl.rb
type_station-0.4.7 lib/type_station/dsl.rb
type_station-0.4.6 lib/type_station/dsl.rb
type_station-0.4.5 lib/type_station/dsl.rb
type_station-0.4.4 lib/type_station/dsl.rb
type_station-0.4.3 lib/type_station/dsl.rb
type_station-0.4.2 lib/type_station/dsl.rb
type_station-0.4.1 lib/type_station/dsl.rb
type_station-0.4.0 lib/type_station/dsl.rb
type_station-0.3.4 lib/type_station/dsl.rb
type_station-0.3.3 lib/type_station/dsl.rb
type_station-0.3.2 lib/type_station/dsl.rb
type_station-0.3.1 lib/type_station/dsl.rb
type_station-0.3.0 lib/type_station/dsl.rb
type_station-0.2.3 lib/type_station/dsl.rb
type_station-0.2.2 lib/type_station/dsl.rb