Sha256: 6b82f500be49c0b7ec63a59962a0562832ddf46f6d7b2768cfdfa2358646e6fe

Contents?: true

Size: 1.34 KB

Versions: 6

Compression:

Stored size: 1.34 KB

Contents

class FeaturedWorkList
  include ActiveModel::Model

  def featured_works_attributes=(attributes_collection)
    attributes_collection = attributes_collection.sort_by { |i, _| i.to_i }.map { |_, attributes| attributes } if attributes_collection.is_a? Hash
    attributes_collection.each do |attributes|
      attributes = attributes.with_indifferent_access
      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
      CurationConcerns::PresenterFactory.build_presenters(ids,
                                                          Sufia::WorkShowPresenter,
                                                          ability)
    end

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
sufia-7.1.0 app/models/featured_work_list.rb
sufia-7.0.0 app/models/featured_work_list.rb
sufia-7.0.0.rc2 app/models/featured_work_list.rb
sufia-7.0.0.rc1 app/models/featured_work_list.rb
sufia-7.0.0.beta4 app/models/featured_work_list.rb
sufia-7.0.0.beta3 app/models/featured_work_list.rb