Sha256: e8943346ed3d53ea34df55bfdce872fc49633156b5995c99c2275d2b221032e7

Contents?: true

Size: 930 Bytes

Versions: 1

Compression:

Stored size: 930 Bytes

Contents

class Nacelle::CellsSerializer
  def as_json(*)
    { cells: cell_classes.flat_map(&method(:serialize)) }
  end

  private

  def serialize klass
    cell = klass.to_s.sub("Cell", "").underscore
    cell_name = cell.humanize

    klass.action_methods.sort.map do |action|
      action_name = action.to_s.humanize
      {
        id:   "#{cell}/#{action}",
        name: "#{cell_name} #{action_name}",
        form: settings_html_for(cell, action),
      }
    end
  end

  def cell_classes
    @all ||= begin
      require_all_cells
      Nacelle::Cell.subclasses
    end
  end

  def require_all_cells
    # TODO pull in cells from engines, too
    cell_files = Dir[::Rails.root.join('app/cells/*.rb')]
    cell_files.each(&method(:require))
  end

  def settings_html_for cell, action
    path = ::Rails.root.join("app/cells/#{cell}/#{action}_form.html.erb")
    ERB.new(File.read(path)).result
  rescue Errno::ENOENT
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nacelle-0.6.0 lib/nacelle/cells_serializer.rb