Sha256: 73a4889862465e55ce5f2f4bfaf16819603b521e8ca7f20e4429e16754276a98

Contents?: true

Size: 1.74 KB

Versions: 38

Compression:

Stored size: 1.74 KB

Contents

module Pageflow
  class ThumbnailFileResolver < Struct.new(:entry, :candidates, :configuration)
    def find_thumbnail
      candidates.detect do |candidate|
        condition = candidate[:unless] || candidate[:if]
        next if condition && !condition_met?(condition, candidate[:unless])

        file = find_positioned_file_by_candiate(candidate)
        break file if file
      end || PositionedFile.null
    end

    private

    def condition_met?(condition, negated)
      if !condition.is_a?(Hash) ||
         condition.keys.sort != [:attribute, :value]
        raise(ArgumentError,
              'Expected thumbnail candidate condition to have keys :attribute and :value.')
      end

      value = configuration[condition[:attribute].to_s]

      if negated
        value != condition[:value]
      else
        value == condition[:value]
      end
    end

    def find_positioned_file_by_candiate(candidate)
      PositionedFile.wrap(find_file_by_candidate(candidate),
                          file_position(candidate, :x),
                          file_position(candidate, :y))
    end

    def find_file_by_candidate(candidate)
      entry.find_file_by_perma_id(file_model(candidate), record_id(candidate))
    end

    def file_model(candidate)
      file_type(candidate).model
    end

    def file_type(candidate)
      Pageflow.config.file_types.find_by_collection_name!(candidate.fetch(:file_collection))
    end

    def record_id(candidate)
      configuration[candidate.fetch(:attribute)]
    end

    def file_position(candidate, coordinate)
      configuration[file_position_attribute(candidate, coordinate)]
    end

    def file_position_attribute(candidate, coordinate)
      candidate.fetch(:attribute).gsub(/_id$/, "_#{coordinate}")
    end
  end
end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
pageflow-17.0.4 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-17.0.3 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-17.0.2 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-17.0.1 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-17.0.0 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-16.2.0 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-16.1.0 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-16.0.0 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-15.8.0 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-15.7.1 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-15.7.0 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-15.6.1 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-15.6.0 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-15.5.0 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-15.4.0 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-15.3.0 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-15.2.2 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-15.2.1 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-15.2.0 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-15.1.2 app/models/pageflow/thumbnail_file_resolver.rb