Sha256: f33eb784c7db1c42a6e9fccfedf047c620faad68fb0e1fc969ca3c6184766da0

Contents?: true

Size: 1.45 KB

Versions: 35

Compression:

Stored size: 1.45 KB

Contents

class FeaturedWorkList
  include ActiveModel::Model

  # @param [ActionController::Parameters] a collection of nested perameters
  def featured_works_attributes=(attributes_collection)
    attributes_collection = attributes_collection.to_h if attributes_collection.respond_to?(:permitted?)
    attributes_collection = attributes_collection.sort_by { |i, _| i.to_i }.map { |_, attributes| attributes } if attributes_collection.is_a? Hash
    attributes_collection.each do |attributes|
      raise "Missing id" if attributes['id'].blank?
      existing_record = FeaturedWork.find(attributes['id'])
      existing_record.update(attributes.except('id'))
    end
  end

  def featured_works
    return @works if @works
    @works = FeaturedWork.all
    add_solr_document_to_works
    @works = @works.reject do |work|
      work.destroy if work.presenter.blank?
      work.presenter.blank?
    end
  end

  delegate :empty?, to: :featured_works

  private

    def add_solr_document_to_works
      work_presenters.each do |presenter|
        work_with_id(presenter.id).presenter = presenter
      end
    end

    def ids
      @works.pluck(:work_id)
    end

    def work_presenters
      ability = nil
      Hyrax::PresenterFactory.build_for(ids: ids,
                                        presenter_class: Hyrax::WorkShowPresenter,
                                        presenter_args: ability)
    end

    def work_with_id(id)
      @works.find { |w| w.work_id == id }
    end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
hyrax-2.9.6 app/models/featured_work_list.rb
hyrax-2.9.5 app/models/featured_work_list.rb
hyrax-2.9.4 app/models/featured_work_list.rb
hyrax-2.9.3 app/models/featured_work_list.rb
hyrax-2.9.2 app/models/featured_work_list.rb
hyrax-2.9.1 app/models/featured_work_list.rb
hyrax-2.9.0 app/models/featured_work_list.rb
hyrax-2.8.0 app/models/featured_work_list.rb
hyrax-2.7.2 app/models/featured_work_list.rb
hyrax-2.7.1 app/models/featured_work_list.rb
hyrax-2.7.0 app/models/featured_work_list.rb
hyrax-2.6.0 app/models/featured_work_list.rb
hyrax-3.0.0.pre.rc1 app/models/featured_work_list.rb
hyrax-3.0.0.pre.beta3 app/models/featured_work_list.rb
hyrax-2.5.1 app/models/featured_work_list.rb
hyrax-2.5.0 app/models/featured_work_list.rb
hyrax-3.0.0.pre.beta2 app/models/featured_work_list.rb
hyrax-2.4.1 app/models/featured_work_list.rb
hyrax-3.0.0.pre.beta1 app/models/featured_work_list.rb
hyrax-2.4.0 app/models/featured_work_list.rb