Class: DcImage

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps
Defined in:
app/models/dc_image.rb

Overview

Schema information

Collection name: dc_images : Images

_id                  BSON::ObjectId     _id
created_at           Time               created_at
updated_at           Time               updated_at
dc_site_id           BSON::ObjectId     Site id
dc_user_id           BSON::ObjectId     User's id
name                 String             ip
short                String             short name
text                 String             text
size_l               String             Large image size
size_m               String             Medium image size
size_s               String             Small image size

DRG CMS module for saveing and manipulating images.

If you want to use this module you must install image magick tools and mini_magick ruby gem.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_usersObject

Return all users that have contributed images



229
230
231
# File 'app/models/dc_image.rb', line 229

def self.all_users
  DcUser.where(:id.in => distinct(:created_by)).order_by(name: 1).map { |doc| [doc.name, doc.id] }
end

.html_codeObject



233
234
235
# File 'app/models/dc_image.rb', line 233

def self.html_code
  'code'
end

Instance Method Details

#categories_lineObject

For mapping categories array to display field



121
122
123
# File 'app/models/dc_image.rb', line 121

def categories_line
  categories ? categories.join(", ") : ''
end

#cats_inputObject

For mapping categories array to input text_area field



114
115
116
# File 'app/models/dc_image.rb', line 114

def cats_input
  self.categories.join("\n")
end

#cats_input=(what) ⇒ Object

For mapping categories array to input text_area field



103
104
105
106
107
108
109
# File 'app/models/dc_image.rb', line 103

def cats_input=(what)
  self.categories = if what.blank?
                      []
                    else
                      what.chomp.split("\n").map(&:downcase).map(&:strip)
                    end
end

#first_available_imageObject

Will return first available image starting from small up



128
129
130
131
132
133
134
# File 'app/models/dc_image.rb', line 128

def first_available_image
  image = %w[o s m l].each do |size|
    field = "size_#{size}"
    value = send(field)
    return "#{id}-#{size}.#{img_type}" if value.present?
  end
end

#offset_lxObject

Will return x offset for cropping large image



153
154
155
# File 'app/models/dc_image.rb', line 153

def offset_lx
  size_l.blank? ? '' : size_l.split(/x|\+/)[2].to_i
end

#offset_lx=(value) ⇒ Object

Will set x offset for cropping large image



160
161
162
# File 'app/models/dc_image.rb', line 160

def offset_lx=(value)
  self.size_l << (size_l.blank? ? '' : "+#{value}")
end

#offset_lyObject

Will return y offset for cropping large image



167
168
169
# File 'app/models/dc_image.rb', line 167

def offset_ly
  size_l.blank? ? '' : size_l.split(/x|\+/)[3].to_i
end

#offset_ly=(value) ⇒ Object

Will set y offset for cropping large image



174
175
176
# File 'app/models/dc_image.rb', line 174

def offset_ly=(value)
  self.size_l << (size_l.blank? ? '' : "+#{value}")
end

#offset_mxObject



186
187
188
# File 'app/models/dc_image.rb', line 186

def offset_mx
  size_m.blank? ? '' : size_m.split(/x|\+/)[2].to_i
end

#offset_mx=(value) ⇒ Object



190
191
192
# File 'app/models/dc_image.rb', line 190

def offset_mx=(value)
  self.size_m << (size_m.blank? ? '' : "+#{value}")
end

#offset_myObject



194
195
196
# File 'app/models/dc_image.rb', line 194

def offset_my
  size_m.blank? ? '' : size_m.split(/x|\+/)[3].to_i
end

#offset_my=(value) ⇒ Object



198
199
200
# File 'app/models/dc_image.rb', line 198

def offset_my=(value)
  self.size_m << (size_m.blank? ? '' : "+#{value}")
end

#offset_sxObject



210
211
212
# File 'app/models/dc_image.rb', line 210

def offset_sx
  size_s.blank? ? '' : size_s.split(/x|\+/)[2].to_i
end

#offset_sx=(value) ⇒ Object



214
215
216
# File 'app/models/dc_image.rb', line 214

def offset_sx=(value)
  self.size_s << (size_s.blank? ? '' : "+#{value}")
end

#offset_syObject



218
219
220
# File 'app/models/dc_image.rb', line 218

def offset_sy
  size_s.blank? ? '' : size_s.split(/x|\+/)[3].to_i
end

#offset_sy=(value) ⇒ Object



222
223
224
# File 'app/models/dc_image.rb', line 222

def offset_sy=(value)
  self.size_s << (size_s.blank? ? '' : "+#{value}")
end

#set_originalObject

checks that image size values are in correct format. Must be hsizevsize (ex. 300x200)



75
76
77
78
79
80
81
82
83
84
# File 'app/models/dc_image.rb', line 75

def set_original
  if keep_original
    if size_o.blank?
      image = MiniMagick::Image.open(name)
      self.size_o = "#{image.width}x#{image.height}"
    end
  else
    self.size_o = ''
  end
end

#size_lsObject

Will return size for large image



139
140
141
# File 'app/models/dc_image.rb', line 139

def size_ls
  size_l.blank? ? '' : size_l.split(/x|\+/)[0, 2].join('x')
end

#size_ls=(value) ⇒ Object

Will set new size for large image



146
147
148
# File 'app/models/dc_image.rb', line 146

def size_ls=(value)
  self.size_l = value.blank? ? '' : value
end

#size_msObject



178
179
180
# File 'app/models/dc_image.rb', line 178

def size_ms
  size_m.blank? ? '' : size_m.split(/x|\+/)[0, 2].join('x')
end

#size_ms=(value) ⇒ Object



182
183
184
# File 'app/models/dc_image.rb', line 182

def size_ms=(value)
  self.size_m = value.blank? ? '' : value
end

#size_ssObject



202
203
204
# File 'app/models/dc_image.rb', line 202

def size_ss
  size_s.blank? ? '' : size_s.split(/x|\+/)[0, 2].join('x')
end

#size_ss=(value) ⇒ Object



206
207
208
# File 'app/models/dc_image.rb', line 206

def size_ss=(value)
  self.size_s = value.blank? ? '' : value
end

#validate_image_valuesObject

checks that image size values are in correct format. Must be hsizevsize (ex. 300x200)



89
90
91
92
93
94
95
96
97
98
# File 'app/models/dc_image.rb', line 89

def validate_image_values
  %w[l m s o].each do |size|
    field = "size_#{size}"
    value = send(field)
    next if value.blank?

    a = value.strip.split(/x|\+/)
    a[0, 2].each { |e| errors.add(field, I18n.t('drgcms.not_valid')) unless e.to_i > 0 }
  end
end