Sha256: 9baa284c6921346e1ad1407d47fa232b781036384ee5384a360294b72f8079c8

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

require 'dimensions-rails/version'
require 'active_support/ordered_options'
require 'rails/railtie'
require 'dimensions'
require 'ostruct'

module Dimensions
  module Rails

    module Helper

      # Use dimensions by @sstephenson to add automatic images size and improve browser rendering.
      #
      # How this improves browser rendering?
      # https://developers.google.com/speed/docs/best-practices/rendering#SpecifyImageDimensions
      #
      def image_tag source, options = {}
        disable_dimensions = options.delete(:dimensions) == false

        unless disable_dimensions or options[:size] or options[:width] or options[:height]
          fs_path = ::Rails.application.assets.find_asset(source)
          fs_path = fs_path.present? ? fs_path.pathname : File.join(::Rails.public_path, source)

          if fs_path.present? and File.exist? fs_path
            options[:width], options[:height] = ::Dimensions.dimensions(fs_path)
          end
        end
        super
      end

    end

    class Railtie < ::Rails::Railtie
      config.dimensions = ActiveSupport::OrderedOptions.new
      config.dimensions.add_size_by_default = true

      initializer 'dimensions.initialize' do
        ActiveSupport.on_load(:action_view) do
          include ::Dimensions::Rails::Helper
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dimensions-rails-1.1.2 lib/dimensions-rails.rb
dimensions-rails-1.1.1 lib/dimensions-rails.rb