Sha256: 27bcbd70ded73822087dc45401b643c40ac6ff9cf9c6e41ff7bd97f748fc2436

Contents?: true

Size: 1.21 KB

Versions: 4

Compression:

Stored size: 1.21 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
        template_name = options[:template] || (parent ? title.parameterize('_') : 'index')
        redirect_to = options[:redirect_to]
        slug = options[:slug]

        model = ::TypeStation::Page.create(title: title, template_name: template_name, 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

4 entries across 4 versions & 1 rubygems

Version Path
type_station-0.0.3 lib/type_station/dsl.rb
type_station-0.0.2 lib/type_station/dsl.rb
type_station-0.0.1 lib/type_station/dsl.rb
type_station-0.0.1.pre lib/type_station/dsl.rb