Module: ClippedAssetRoles::AssetTagExtensions

Includes:
Radiant::Taggable
Defined in:
lib/clipped_asset_roles/asset_tag_extensions.rb

Defined Under Namespace

Classes: TagError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
11
# File 'lib/clipped_asset_roles/asset_tag_extensions.rb', line 6

def self.included(base)
  base.class_eval {
    alias_method_chain :assets_find_options, :roles
    alias_method_chain :find_asset, :roles
  }
end

Instance Method Details

#assets_find_options_with_roles(tag) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/clipped_asset_roles/asset_tag_extensions.rb', line 54

def assets_find_options_with_roles(tag)
  options = assets_find_options_without_roles(tag)
  roles = tag.attr['roles'].to_s.split('|')
  if roles.any?
    if options[:conditions]
      options[:conditions].concat [ roles.map { |role| "asset_roles.role = ?" }.join(' OR '),
        *roles.map { |role| role } ]
    else
      options[:conditions] = [ roles.map { |role| "asset_roles.role = ?" }.join(' OR '),
        *roles.map { |role| role } ]
    end
    
    options[:joins] = "INNER JOIN `page_attachments` pa ON `assets`.id = pa.asset_id INNER JOIN `asset_roles` ON (`asset_roles`.page_attachment_id = pa.id)"
  end
  options
end

#find_asset_with_roles(tag, options) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/clipped_asset_roles/asset_tag_extensions.rb', line 41

def find_asset_with_roles(tag, options)
  if tag.locals.asset.nil? and roles = options.delete('roles')
    roles.split('|').each do |role|
      tag.locals.asset = tag.locals.page.attachments_with_role(role).first.try(:asset)
      return tag.locals.asset unless tag.locals.asset.nil?
    end
    raise(TagError, "Asset not found.") unless tag.locals.asset
  else
    find_asset_without_roles(tag, options)
  end
  tag.locals.asset
end