Sha256: fb369bc7d60e179b3f7ee4c5fc092624e4af6bb9ee9b13931e1d253e36f398f4

Contents?: true

Size: 1.32 KB

Versions: 3

Compression:

Stored size: 1.32 KB

Contents

module Slices
  class AvailableSlices

    def self.all
      # We've memoized the slices so that we never try and load them all
      # twice. If we iterate over ObjectSpace twice (see find_all_slices)
      # we sometimes find that an incomplete set of slices objects is
      # returned on subsequent calls.
      @all ||= find_all_slices
    end

    private

    def self.find_all_slices
      slices = []

      Dir.glob(File.join(Rails.root, 'app/slices/**/*_slice.rb')).each do |path|

        slice_name = File.basename(path, '.rb')
        klass      = slice_name.camelize.constantize
        basename   = File.basename(path, '_slice.rb')
        folder     = File.basename(File.dirname(path))

        fields = {
          'name' => basename.humanize.titleize,
          'template' => File.join(folder, basename),
          'restricted' => klass.restricted?
        }

        klass.fields.each do |name, field|
          next if Slice.fields[name]
          fields[name] = field.default_val
        end

        if klass.respond_to?(:attachment_fields)
          klass.attachment_fields.each do |name|
            meta = klass.reflect_on_association(name)
            fields[name.to_s] = meta.many? ? [] : nil
          end
        end

        slices << [basename, fields]

      end

      Hash[*slices.sort.flatten]
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
slices-2.0.2 lib/slices/available_slices.rb
slices-2.0.1 lib/slices/available_slices.rb
slices-2.0.0 lib/slices/available_slices.rb