Class: Iformat

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/iformat.rb

Constant Summary collapse

SIZES =
['keep', 'limit', 'force']
GRAVITY =
['CenterGravity', 'NorthWestGravity', 'NorthGravity', 'NorthEastGravity', 'WestGravity', 'EastGravity', 'SouthWestGravity', 'SouthGravity', 'SouthEastGravity']

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](fmt) ⇒ Object



11
12
13
# File 'app/models/iformat.rb', line 11

def [](fmt)
  Thread.current[:visitor].site.iformats[fmt]
end

.formats_for_site(site_id, as_hash = true) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/iformat.rb', line 41

def formats_for_site(site_id, as_hash = true)
  formats = Zena::Use::ImageBuilder::DEFAULT_FORMATS.dup

  site_formats = {}
  last_update = nil

  self.find(:all, :conditions=>["site_id = ?", site_id]).each do |f|
    last_update  = f.updated_at if !last_update || f.updated_at > last_update
    if as_hash
      site_formats[f.name] = f.as_hash
    else
      site_formats[f.name] = f
    end
  end

  formats.merge!(site_formats)
  formats[:updated_at] = last_update
  formats
end

.listObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/iformat.rb', line 15

def list
  res = []
  Thread.current[:visitor].site.iformats.merge(formats_for_site(visitor.site.id, false)).each do |k,v|
    next if k == :updated_at || k.nil?
    if v.kind_of?(Iformat)
      res << v
    else
      res << Iformat.new_from_default(k)
    end
  end
  res.sort do |a,b|
    if a.size == 'keep'
      b.size == 0 ? a[:name] <=> b[:name] : 1
    elsif b.size == 'keep'
      -1
    else
      sz = (a.width.to_f * a.height.to_f) <=> (b.width.to_f * b.height.to_f)
      if sz == 0
        a[:name] <=> b[:name]
      else
        sz
      end
    end
  end
end

.new_from_default(key) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/iformat.rb', line 61

def new_from_default(key)
  return nil unless default = Zena::Use::ImageBuilder::DEFAULT_FORMATS[key]
  obj = self.new
  default.each do |k,v|
    next if k == :hash_id
    if k == :popup && v.kind_of?(Hash)
      v = v.dup
      obj.popup = "#{v.delete(:name)} #{v.to_json}"
    else
      obj.send("#{k}=", v.to_s)
    end
  end
  obj
end

Instance Method Details

#as_hashObject

:size=>:force, :width=>280, :height=>120, :gravity=>Magick::NorthGravity



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/models/iformat.rb', line 78

def as_hash
  if self[:popup]
    if self[:popup] =~ /^(\w+?)\s*\((.*)\)/
      popup = {:name => $1}
      show = $2.split(',').map(&:strip)
      options = {}
      popup[:show] = show.map do |k|
        if k == 'link'
          options['title'] = 'link'
          'title'
        else
          k
        end
      end
      popup[:options] = options
    elsif self[:popup] =~ /^(\w+?)\s*(\{.*\})\s*$/
      popup = {:name => $1}
      options = JSON.load($2) rescue {}
      unless popup[:show] = options.delete('show')
        popup[:show] = options.keys.sort do |a,b|
          # keep sort order
          self[:popup].index(a) <=> self[:popup].index(b)
        end
        popup[:options] = {}
        options.each do |k,v|
          next if v == true
          popup[:options][k] = v
        end
      end
    else
      popup = {
        :name    => self[:popup],
        :options => {'title' => 'link'},
        :show    => ['navigation','title','summary']
      }
    end
  else
    popup = nil
  end

  h = {
    :name    => self[:name],
    :size    => size.to_sym,
    :width   => width,
    :height  => height,
    :gravity => eval("Magick::#{gravity}"),
  }
  h.merge!(:hash_id => Zena::Use::ImageBuilder.hash_id(h))
  h.merge!(:popup => popup) if popup
  h
end

#gravityObject



148
149
150
# File 'app/models/iformat.rb', line 148

def gravity
  GRAVITY[self[:gravity].to_i]
end

#gravity=(str) ⇒ Object



152
153
154
# File 'app/models/iformat.rb', line 152

def gravity=(str)
  self[:gravity] = GRAVITY.index(str)
end

#hash_idObject

This is a unique identifier used to cache images with format: image30_pv.jpg#+ format.hash_id



132
133
134
# File 'app/models/iformat.rb', line 132

def hash_id
  Zena::Use::ImageBuilder.hash_id(self.as_hash)
end


140
141
142
# File 'app/models/iformat.rb', line 140

def popup_name
  popup ? popup[/^(.*?)(\s|$)/,1] : ''
end

#pseudo_idObject



156
157
158
# File 'app/models/iformat.rb', line 156

def pseudo_id
  new_record? ? name : id
end

#sizeObject



136
137
138
# File 'app/models/iformat.rb', line 136

def size
  SIZES[self[:size].to_i]
end

#size=(str) ⇒ Object



144
145
146
# File 'app/models/iformat.rb', line 144

def size=(str)
  self[:size] = SIZES.index(str.to_s)
end