Sha256: c17225cd154ece4829e433374894a3439b985155c4fba1951d306f0b0674d418

Contents?: true

Size: 1.61 KB

Versions: 4

Compression:

Stored size: 1.61 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
      @main_section = nil
      create_sections
      render_sections
    end

    def all_sections
      Array.wrap(@sections.try(:values))
    end

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

    def create_section(name, options)
      section_class = class_factory("#{name}_section")
      options = normalize_options(options).merge(screen: self)
      !options.has_key?(:if) || options[:if] ? section_class.new(options) : nil
    end

    def set_section(name, options = {})
      @main_section = create_section(name, options)
      @main_section.render
    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

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
motion-prime-0.9.3 motion-prime/screens/_sections_mixin.rb
motion-prime-0.9.2 motion-prime/screens/_sections_mixin.rb
motion-prime-0.9.1 motion-prime/screens/_sections_mixin.rb
motion-prime-0.9.0 motion-prime/screens/_sections_mixin.rb