Sha256: e5577298a5ee5c4ca52573defa6e48a24322b8b762149b575720a5d342ee3b40

Contents?: true

Size: 1.19 KB

Versions: 9

Compression:

Stored size: 1.19 KB

Contents

module FactoryBurgers
  module Models
    # Data class for the output of a factory create action
    class FactoryOutput
      attr_reader :object, :presenter

      # `object` is whatever the output of `Factory#create` was
      def initialize(object)
        @object = object
        @presenter = FactoryBurgers::Presenters.presenter_for(object)
      end

      def data
        # TODO: Sorting by name is a UI concern; move it there.
        sorted_assocs = association_factories.sort_by { |item| item[:association].name }
        # TODO: group type, attributes, and link into `object`, move keys into presenter
        return {
          type: presenter.type,
          attributes: presenter.attributes,
          link: presenter.link_path,
          association_factories: sorted_assocs.map { |item| association_factory_data(item) },
        }
      end

      private

      def association_factories
        FactoryBurgers::Introspection.association_factories(object.class)
      end

      def association_factory_data(assoc_factory)
        {
          association_name: assoc_factory[:association].name.to_s,
          factory_name: assoc_factory[:factory].name.to_s,
        }
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
factory_burgers-1.1.3 lib/factory_burgers/models/factory_output.rb
factory_burgers-1.1.2 lib/factory_burgers/models/factory_output.rb
factory_burgers-1.1.1 lib/factory_burgers/models/factory_output.rb
factory_burgers-1.1.0 lib/factory_burgers/models/factory_output.rb
factory_burgers-1.0.0 lib/factory_burgers/models/factory_output.rb
factory_burgers-0.1.5 lib/factory_burgers/models/factory_output.rb
factory_burgers-0.1.4 lib/factory_burgers/models/factory_output.rb
factory_burgers-0.1.2 lib/factory_burgers/models/factory_output.rb
factory_burgers-0.1.0 lib/factory_burgers/models/factory_output.rb