Class: Cms::Asset

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Taggable
Defined in:
app/models/cms/asset.rb

Defined Under Namespace

Classes: Meta

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Taggable

included

Class Method Details

.context_tags(context) ⇒ Object



66
67
68
69
70
71
72
# File 'app/models/cms/asset.rb', line 66

def self.context_tags(context)
  if context
    Tag.all :joins => 'inner join taggings on taggings.tag_id = tags.id inner join cms_assets on taggings.taggable_id = cms_assets.id', :conditions => {'cms_assets.context_id' => context.id}
  else
    Tag.all
  end
end

.tags_for_context(context) ⇒ Object



10
11
12
13
# File 'app/models/cms/asset.rb', line 10

def self.tags_for_context(context)
  common_options = {:order => 'name ASC'}
  context.object ? Cms::Tag.all({:include => :assets, :conditions => {'cms_assets.context_id' => context.object.id}}.merge(common_options)) : Cms::Tag.all(common_options)
end

Instance Method Details

#assign_ordered_attributes(params) ⇒ Object



58
59
60
61
62
63
64
# File 'app/models/cms/asset.rb', line 58

def assign_ordered_attributes(params)
  # force the custom dimensions to be assigned first so that when the asset is assigned, the custom dims are present
  # if the custom dims aren't set before the asset is assigned, the custom size won't be generated properly
  # this can occur if the attribute hash is iterated with the asset coming before the dims
  self.attributes = params.slice(:custom_height, :custom_width)
  self.attributes = params
end

#editable?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'app/models/cms/asset.rb', line 83

def editable?
  Cms::Component.editable?(asset_file_name)
end

#file_contentObject



87
88
89
# File 'app/models/cms/asset.rb', line 87

def file_content
  read
end

#file_content=(content) ⇒ Object



91
92
93
# File 'app/models/cms/asset.rb', line 91

def file_content=(content)
  write content
end

#icon?Boolean

Returns:

  • (Boolean)


78
79
80
81
# File 'app/models/cms/asset.rb', line 78

def icon?
  # accepts ico or icon
  !(asset_content_type =~ /icon?$/).blank?
end

#image?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'app/models/cms/asset.rb', line 74

def image?
  !(asset_content_type =~ /^image.*/).blank?
end

#metaObject



95
96
97
98
# File 'app/models/cms/asset.rb', line 95

def meta
  return @_meta if @_meta.present?
  @_meta = ( || []).collect{|m| Meta.new(m)}
end

#meta=(data) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/models/cms/asset.rb', line 100

def meta=(data)
  # reset the cached meta collection
  @_meta = nil

  # data ex:
  # {"new_1301457489798"=>{"name"=>"test1", "value"=>"test1"}, "new_1301457493800"=>{"name"=>"test2", "value"=>"test2"}}
  # converted to:
  # [{"name"=>"test1", "value"=>"test1"}, {"name"=>"test2", "value"=>"test2"}]
  # strip spaces of name and value
  temp_data = data.to_a.sort{|a,b| a.first <=> b.first}.collect{|a| h = a[1]; {:name => h[:name].strip, :value => h[:value].strip} }
  # remove any elements that have both name and value blank
  temp_data = temp_data.reject{|d| d[:name].blank? && d[:value].blank?}

  self. = temp_data
end

#to_liquidObject



116
117
118
# File 'app/models/cms/asset.rb', line 116

def to_liquid
  Cms::AssetDrop.new(self)
end

#to_sObject



54
55
56
# File 'app/models/cms/asset.rb', line 54

def to_s
  asset_file_name
end