Module: PlainRecord::Extra::Image

Defined in:
lib/plain_record/extra/image.rb

Overview

Extention to store images.

It make sense only with ‘entry_in` models. You can get created or modified time from first or last git commit time.

It is additional extention, so you need to include ‘PlainRecord::Extra::Git` module to your model.

class User
  include PlainRecord::Resource
  include PlainRecord::Extra::Image

  entry_in   "users/*.yml"

  image_from do |user, field|
    "users/#{field}/#{user.name}.png"
  end
  image_url do |user, field, size|
    if size
      "users/#{user.name}/#{field}.#{size}.png"
    else
      "users/#{user.name}/#{field}.png"
    end
  end

  virtual :name,   in_filepath(1)
  virtual :avatar, image(small: '32x32', big: '64x64')
  virtual :photo,  image
end

# There are images at `data/users/avatar/ai.png` and
# `data/users/photo/ai.png`

PlainRecord::Extra::Image.convert_images!
user = User.first(name: 'ai')

user.photo.url  #=> "data/users/ai/photo.png"
user.photo.file #=> "app/assets/images/data/users/ai/avatar.small.png"
user.avatar(:small).url  #=> "data/users/ai/avatar.small.png"

Defined Under Namespace

Modules: Model Classes: Data

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.dirObject

Base of converted images. Set to app/aseets/images/data in Rails.



70
71
72
# File 'lib/plain_record/extra/image.rb', line 70

def dir
  @dir
end

.included_inObject

Models, that used this extention.



66
67
68
# File 'lib/plain_record/extra/image.rb', line 66

def included_in
  @included_in
end

.urlObject

Base of converted images URL. Set to data/ in Rails.



73
74
75
# File 'lib/plain_record/extra/image.rb', line 73

def url
  @url
end

Class Method Details

.clean_images!Object

Delete all converted images.



103
104
105
106
107
# File 'lib/plain_record/extra/image.rb', line 103

def clean_images!
  Dir.glob(File.join(self.dir, '**/*')) do |file|
    FileUtils.rm_r(file) if File.exists? file
  end
end

.convert_images!Object

Convert images in all models.



110
111
112
113
114
115
116
117
# File 'lib/plain_record/extra/image.rb', line 110

def convert_images!
  clean_images!
  return unless included_in

  included_in.each do |model|
    model.all.each { |i| i.convert_images! }
  end
end

.included(base) ⇒ Object



75
76
77
78
79
# File 'lib/plain_record/extra/image.rb', line 75

def included(base)
  base.send :extend, Model
  self.included_in ||= []
  self.included_in << base
end

.install(klass) ⇒ Object

Define class variables.



82
83
84
# File 'lib/plain_record/extra/image.rb', line 82

def install(klass)
  klass.image_sizes = { }
end

Instance Method Details

#convert_images!Object

Convert all images to public dir.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/plain_record/extra/image.rb', line 122

def convert_images!
  self.class.image_sizes.each_pair do |field, sizes|
    from = self.class.get_image_from(self, field)
    next unless File.exists? from

    if sizes.empty?
      to = self.class.get_image_file(self, field, nil)
      FileUtils.mkpath(File.dirname(to))
      FileUtils.cp(from, to)
    else
      require 'RMagick'
      source = ::Magick::Image.read(from).first
      sizes.each_pair do |name, size|
        to    = self.class.get_image_file(self, field, name)
        w, h  = size.split('x')
        image = source.resize(w.to_i, h.to_i)
        self.class.use_callbacks(:convert_image, self, to) do
          FileUtils.mkpath(File.dirname(to))
          image.write(to)
        end
      end
    end
  end
end