Sha256: 8d08087d912daae0d4c1f7c37740d2e155b1e9aee96626dda25046031d7367b6

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

module MotionPrime
  module ScreenSectionsMixin
    extend ::MotionSupport::Concern

    include HasClassFactory
    include HasNormalizer

    def self.included(base)
      base.class_attribute :_section_options
    end

    def add_sections
      create_sections
      render_sections
    end

    def all_sections
      @sections.values
    end

    def create_sections
      section_options = self.class._section_options
      return unless section_options
      @sections = {}
      section_options.map do |name, options|
        @sections[name] = create_section(options.clone)
      end
    end

    def create_section(options)
      section_class = class_factory("#{options.delete(:name)}_section")
      options = normalize_options(options).merge(screen: self)
      section_class.new(options)
    end

    def render_sections
      return unless @sections
      if all_sections.count > 1
        @main_section = MotionPrime::TableSection.new(model: all_sections, screen: self)
        @main_section.render
      else
        all_sections.first.render
      end
    end

    def main_section
      @main_section || all_sections.first
    end

    module ClassMethods
      def section(name, options = {})
        self._section_options ||= {}
        self._section_options[name.to_sym] = options.merge(name: name)

        define_method name do
          @sections[name]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
motion-prime-0.8.0 motion-prime/screens/_sections_mixin.rb