Sha256: ee4b036eb20e92ddf50ba2f55d7aeeda9ffa93894983424043eefff178884a7b

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

require 'nokogiri'
module ActiveadminSelleoCms
  class Layout

    def initialize(path)
      @path = path
      @file = File.read(path)
      @doc = Nokogiri::HTML(@file)
    end

    def section_names
      @section_names ||= sections.map(&:name)
    end

    def sections
      @sections ||= @doc.css('section[name]').map do |node|
        Section.new(node)
      end
    end

    def find_section(name)
      sections.detect{|section| section.name == name}
    end

    class << self
      def find(name)
        begin
          Layout.new(Dir.glob(File.join(Rails.root, "app/views/layouts/#{name}.html.erb")).first)
        rescue
          nil
        end
      end

      def all
        Dir.glob(File.join(Rails.root, 'app/views/layouts/[a-z]*html.erb')).map{|l| l.split('/').last.split('.').first }
      end
    end

    class Section
      attr_accessor :name, :type, :toolbar, :attachments

      def initialize(node)
        @name = node.attributes["name"].content
        @attachments = node.attributes["attachments"] ? true : false
        @type = node.attributes["data-type"] ? node.attributes["data-type"].content : 'ckeditor'
        @toolbar = node.attributes["data-toolbar"] ? node.attributes["data-toolbar"].content : 'Easy'
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
activeadmin-selleo-cms-0.0.18 app/models/activeadmin_selleo_cms/layout.rb
activeadmin-selleo-cms-0.0.17 app/models/activeadmin_selleo_cms/layout.rb
activeadmin-selleo-cms-0.0.16 app/models/activeadmin_selleo_cms/layout.rb