0001-Adds-asset_id-parameters-to-assets.patch

Hiroshi Shirosaki, 2016-12-15 09:54

Download (2.25 KB)

View differences:

config/initializers/10-patches.rb
187 187
    end
188 188
  end
189 189
end
190

  
191
# Adds asset_id parameters to assets like Rails 3 to invalidate caches in browser
192
module ActionView
193
  module Helpers
194
    module AssetUrlHelper
195
      @@cache_asset_timestamps = Rails.env.production?
196
      @@asset_timestamps_cache = {}
197
      @@asset_timestamps_cache_guard = Mutex.new
198

  
199
      def asset_path_with_asset_id(source, options = {})
200
        asset_id = rails_asset_id(source, options)
201
        unless asset_id.blank?
202
          source += "?#{asset_id}"
203
        end
204
        asset_path(source, options)
205
      end
206
      alias :path_to_asset :asset_path_with_asset_id
207

  
208
      def rails_asset_id(source, options = {})
209
        if asset_id = ENV["RAILS_ASSET_ID"]
210
          asset_id
211
        else
212
          if @@cache_asset_timestamps && (asset_id = @@asset_timestamps_cache[source])
213
            asset_id
214
          else
215
            extname = compute_asset_extname(source, options)
216
            path = File.join(Rails.public_path, "#{source}#{extname}")
217
            exist = false
218
            if File.exist? path
219
              exist = true
220
            else
221
              path = File.join(Rails.public_path, compute_asset_path("#{source}#{extname}", options))
222
              if File.exist? path
223
                exist = true
224
              end
225
            end
226
            asset_id = exist ? File.mtime(path).to_i.to_s : ''
227

  
228
            if @@cache_asset_timestamps
229
              @@asset_timestamps_cache_guard.synchronize do
230
                @@asset_timestamps_cache[source] = asset_id
231
              end
232
            end
233

  
234
            asset_id
235
          end
236
        end
237
      end
238
    end
239
  end
240
end
190
-