Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions lib/stylus/tilt/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,43 @@ def evaluate(scope, locals, &block)

# Internal: Builds body of a mixin
#
# Returns string representation of a mixin with asset helper functions
# Returns string representation of all asset helper mixins
def build_mixin_body(scope)
@mixin_body ||= if assets_hash(scope).values.all? {|value| value != '' }
<<-STYL
asset-url(key)
return pair[1] if pair[0] == key for pair in #{assets_hash(scope)[:url]} ()
asset-path(key)
return pair[1] if pair[0] == key for pair in #{assets_hash(scope)[:path]} ()
#{generate_helper(scope, 'asset')}
#{generate_helper(scope, 'image')}
#{generate_helper(scope, 'audio')}
#{generate_helper(scope, 'video')}
STYL
else
''
end
end

# Internal: Generates stylus mixin pairs for asset types
#
# Returns string representations of mixins in stylus syntax
def generate_helper(scope, name)
res = ""
['url', 'path'].each do |type|
asset_pair = assets_hash(scope)[type.to_sym]
asset_pair = assets_hash(scope, type: name)[type.to_sym] if name != 'asset'
res += <<-EOS
#{name}-#{type}(key)
return pair[1] if pair[0] == key for pair in #{asset_pair} ()
EOS
end
return res
end

# Internal: Construct Hash with absolute/relative paths in stylus syntax.
#
# Returns string representations of hash in Stylus syntax
def assets_hash(scope)
def assets_hash(scope, options = {})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This options Hash won't do much effect since this method memoizes the first call into a instance variable, I guess we need to rework that a bit. One option would be to memoize the calls based on the given options on a Hash, like @assets_hashes[options] ||=.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, I'm not entirely sure I understand what that first line is doing. If you could be a little more specific with the fix I might be able to better understand and add this in, or maybe you can make a commit that resolves this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the memoization (@assets_hash ||=) is used so the whole logic is executed once and cached in that instance variable. This works fine if the subsequent method calls are using the same arguments (or none), but that isn't the case anymore.

a_hash = assets_hash(scope)
# on this second call the same result of the previous call will be returned, regardless
# of the different options.
a_hash = assets_hash(scope, type: 'image')

For the options Hash to have any effect this memoization needs to go away or the results should be cached differently.

@assets_hash ||= scope.environment.each_logical_path.each_with_object({ :url => '', :path => '' }) do |logical_path, assets_hash|
unless logical_path =~/.*\.(css|js)$/
path_to_asset = scope.path_to_asset(logical_path)
path_to_asset = scope.path_to_asset(logical_path, options)
assets_hash[:url] << "('#{logical_path}' url(#{path_to_asset})) "
assets_hash[:path] << "('#{logical_path}' \"#{path_to_asset}\") "
end
Expand Down