Sha256: acae4e2518554242c2f8a17d710def1b50e66e31d585195e4adae4cb65f535a5

Contents?: true

Size: 1.71 KB

Versions: 49

Compression:

Stored size: 1.71 KB

Contents

module Pageflow
  class ThumbnailFileResolver < Struct.new(:candidates, :configuration)
    def find
      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)
      file_model(candidate).find_by_id(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

49 entries across 49 versions & 1 rubygems

Version Path
pageflow-14.5.2 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-14.5.1 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-14.5.0 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-14.4.0 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-14.3.0 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-14.2.1 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-14.2.0 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-14.1.1 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-14.1.0 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-14.0.0 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-14.0.0.rc2 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-14.0.0.rc1 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-14.0.0.beta3 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-14.0.0.beta2 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-14.0.0.beta1 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-13.6.0 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-13.5.0 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-13.4.0 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-13.3.0 app/models/pageflow/thumbnail_file_resolver.rb
pageflow-13.2.0 app/models/pageflow/thumbnail_file_resolver.rb