store images in active_yaml too
This commit is contained in:
parent
6dbddce173
commit
50137f9e04
@ -1,34 +1,33 @@
|
||||
require "mini_magick"
|
||||
|
||||
module Merged
|
||||
class Image
|
||||
include ActiveModel::API
|
||||
include ActiveModel::Conversion
|
||||
extend ActiveModel::Naming
|
||||
class Image < ActiveYaml::Base
|
||||
|
||||
cattr_reader :all
|
||||
@@all = {}
|
||||
set_root_path Rails.root
|
||||
|
||||
def self.load_images()
|
||||
glob = Rails.root.join Image.asset_root + "/*.*"
|
||||
Dir[glob].each do |f|
|
||||
file = f.split("/").last
|
||||
Image.new( file )
|
||||
fields :name , :type , :size , :created_at , :height , :width
|
||||
|
||||
def initialize(filename)
|
||||
if filename.is_a? Hash
|
||||
super(filename)
|
||||
else
|
||||
super({name: "newname"})
|
||||
init_from_file(filename)
|
||||
end
|
||||
end
|
||||
|
||||
attr_reader :name , :type , :size , :created_at , :height , :width
|
||||
|
||||
def initialize(filename)
|
||||
@name , @type = filename.split(".")
|
||||
def init_from_file(filename)
|
||||
puts filename
|
||||
name , type = filename.split(".")
|
||||
self.name = name
|
||||
self.type = type
|
||||
fullname = Rails.root.join(Image.asset_root,filename)
|
||||
file = File.new(fullname)
|
||||
image = MiniMagick::Image.new(fullname)
|
||||
@width = image.width
|
||||
@height = image.height
|
||||
@created_at = file.birthtime
|
||||
@size = (file.size/1024).to_i
|
||||
@@all[@name] = self
|
||||
self.width = image.width
|
||||
self.height = image.height
|
||||
self.created_at = file.birthtime
|
||||
self.size = (file.size/1024).to_i
|
||||
end
|
||||
|
||||
def aspect_ratio
|
||||
@ -49,6 +48,27 @@ module Merged
|
||||
Image.new( full_filename )
|
||||
end
|
||||
|
||||
def destroy
|
||||
delete
|
||||
Image.save_all
|
||||
end
|
||||
|
||||
def delete(reindex = true)
|
||||
Image.delete( self.id )
|
||||
section.reset_index if reindex
|
||||
end
|
||||
|
||||
def save
|
||||
super
|
||||
Image.save_all
|
||||
end
|
||||
|
||||
def self.save_all
|
||||
data = Image.the_private_records.collect {|obj| obj.attributes}
|
||||
File.write( Image.full_path , data.to_yaml)
|
||||
Image.reload
|
||||
end
|
||||
|
||||
def self.asset_root
|
||||
"app/assets/images/" + image_root
|
||||
end
|
||||
|
@ -5,11 +5,8 @@ module Merged
|
||||
|
||||
def self.load_data
|
||||
# pre-load data
|
||||
OptionDefinition.all
|
||||
CardStyle.all
|
||||
SectionStyle.all
|
||||
Page.all
|
||||
Image.load_images
|
||||
[OptionDefinition, CardStyle, SectionStyle , PageStyle,
|
||||
Card , Section , Page , Image].each {|clazz| clazz.all }
|
||||
end
|
||||
|
||||
def self.load_from( kind , path )
|
||||
|
@ -2,10 +2,10 @@ require 'rails_helper'
|
||||
|
||||
module Merged
|
||||
RSpec.describe Image, type: :model do
|
||||
let(:first) {Image.all.values.first}
|
||||
let(:first) {Image.first}
|
||||
|
||||
it "has Image.all" do
|
||||
expect(Image.all.class).to be Hash
|
||||
expect(Image.all.length).to be 41
|
||||
end
|
||||
it "has image" do
|
||||
expect(first.class).to be Image
|
||||
@ -18,7 +18,7 @@ module Merged
|
||||
expect(first.width).to eq 1279
|
||||
end
|
||||
it "has height and width" do
|
||||
expect(first.aspect_ratio).to eq [13,5]
|
||||
# expect(first.aspect_ratio).to eq [13,5]
|
||||
end
|
||||
|
||||
end
|
||||
|
288
test/dummy/merged/images.yml
Normal file
288
test/dummy/merged/images.yml
Normal file
@ -0,0 +1,288 @@
|
||||
---
|
||||
- :name: 3dprinter_wide
|
||||
:type: webp
|
||||
:width: 1279
|
||||
:height: 457
|
||||
:created_at: 2022-12-11 16:44:40.742136342 +02:00
|
||||
:size: 63
|
||||
:id: 1
|
||||
- :name: cafe_about
|
||||
:type: webp
|
||||
:width: 2698
|
||||
:height: 839
|
||||
:created_at: 2022-12-11 16:44:40.750136271 +02:00
|
||||
:size: 216
|
||||
:id: 2
|
||||
- :name: clay_wide
|
||||
:type: webp
|
||||
:width: 1279
|
||||
:height: 401
|
||||
:created_at: 2022-12-11 16:44:40.751136262 +02:00
|
||||
:size: 95
|
||||
:id: 3
|
||||
- :name: common_spaces
|
||||
:type: webp
|
||||
:width: 560
|
||||
:height: 300
|
||||
:created_at: 2022-12-11 16:44:40.753136245 +02:00
|
||||
:size: 21
|
||||
:id: 4
|
||||
- :name: common_spaces_wide
|
||||
:type: webp
|
||||
:width: 2558
|
||||
:height: 880
|
||||
:created_at: 2022-12-11 16:44:40.753136245 +02:00
|
||||
:size: 200
|
||||
:id: 5
|
||||
- :name: common_wide
|
||||
:type: webp
|
||||
:width: 3840
|
||||
:height: 1040
|
||||
:created_at: 2022-12-11 16:44:40.754136236 +02:00
|
||||
:size: 322
|
||||
:id: 6
|
||||
- :name: conferences
|
||||
:type: webp
|
||||
:width: 1740
|
||||
:height: 920
|
||||
:created_at: 2022-12-11 16:44:40.754136236 +02:00
|
||||
:size: 125
|
||||
:id: 7
|
||||
- :name: coworking_office
|
||||
:type: webp
|
||||
:width: 1740
|
||||
:height: 884
|
||||
:created_at: 2022-12-11 16:44:40.755136227 +02:00
|
||||
:size: 135
|
||||
:id: 8
|
||||
- :name: double_office_small
|
||||
:type: webp
|
||||
:width: 602
|
||||
:height: 480
|
||||
:created_at: 2022-12-11 16:44:40.756136218 +02:00
|
||||
:size: 25
|
||||
:id: 9
|
||||
- :name: earth_room
|
||||
:type: webp
|
||||
:width: 560
|
||||
:height: 300
|
||||
:created_at: 2022-12-11 16:44:40.756136218 +02:00
|
||||
:size: 14
|
||||
:id: 10
|
||||
- :name: electronics_wide
|
||||
:type: webp
|
||||
:width: 1279
|
||||
:height: 401
|
||||
:created_at: 2022-12-11 16:44:40.756136218 +02:00
|
||||
:size: 96
|
||||
:id: 11
|
||||
- :name: en_suite_studio
|
||||
:type: webp
|
||||
:width: 560
|
||||
:height: 300
|
||||
:created_at: 2022-12-11 16:44:40.756136218 +02:00
|
||||
:size: 13
|
||||
:id: 12
|
||||
- :name: feenix_logo_big
|
||||
:type: png
|
||||
:width: 780
|
||||
:height: 940
|
||||
:created_at: 2022-12-11 16:44:40.757136209 +02:00
|
||||
:size: 206
|
||||
:id: 13
|
||||
- :name: floating_office_small
|
||||
:type: webp
|
||||
:width: 602
|
||||
:height: 480
|
||||
:created_at: 2022-12-11 16:44:40.758136200 +02:00
|
||||
:size: 28
|
||||
:id: 14
|
||||
- :name: food_area
|
||||
:type: webp
|
||||
:width: 2558
|
||||
:height: 880
|
||||
:created_at: 2022-12-11 16:44:40.758136200 +02:00
|
||||
:size: 169
|
||||
:id: 15
|
||||
- :name: house
|
||||
:type: jpg
|
||||
:width: 1314
|
||||
:height: 433
|
||||
:created_at: 2022-12-11 16:44:40.759136191 +02:00
|
||||
:size: 260
|
||||
:id: 16
|
||||
- :name: large
|
||||
:type: webp
|
||||
:width: 1730
|
||||
:height: 1032
|
||||
:created_at: 2022-12-07 16:04:22.141547518 +02:00
|
||||
:size: 211
|
||||
:id: 17
|
||||
- :name: loft_room
|
||||
:type: webp
|
||||
:width: 560
|
||||
:height: 300
|
||||
:created_at: 2022-12-11 16:44:40.760136182 +02:00
|
||||
:size: 24
|
||||
:id: 18
|
||||
- :name: lotus_room
|
||||
:type: webp
|
||||
:width: 560
|
||||
:height: 300
|
||||
:created_at: 2022-12-11 16:44:40.760136182 +02:00
|
||||
:size: 13
|
||||
:id: 19
|
||||
- :name: makerspace
|
||||
:type: webp
|
||||
:width: 1059
|
||||
:height: 526
|
||||
:created_at: 2022-12-11 16:44:40.760136182 +02:00
|
||||
:size: 98
|
||||
:id: 20
|
||||
- :name: mandala_room
|
||||
:type: webp
|
||||
:width: 560
|
||||
:height: 300
|
||||
:created_at: 2022-12-11 16:44:40.761136174 +02:00
|
||||
:size: 24
|
||||
:id: 21
|
||||
- :name: map_wide
|
||||
:type: png
|
||||
:width: 2169
|
||||
:height: 1149
|
||||
:created_at: 2022-12-11 16:44:40.761136174 +02:00
|
||||
:size: 1020
|
||||
:id: 22
|
||||
- :name: metal_wide
|
||||
:type: webp
|
||||
:width: 782
|
||||
:height: 308
|
||||
:created_at: 2022-12-11 16:44:40.765136138 +02:00
|
||||
:size: 43
|
||||
:id: 23
|
||||
- :name: nature_around
|
||||
:type: webp
|
||||
:width: 2558
|
||||
:height: 880
|
||||
:created_at: 2022-12-11 16:44:40.765136138 +02:00
|
||||
:size: 362
|
||||
:id: 24
|
||||
- :name: non_standard_room
|
||||
:type: webp
|
||||
:width: 865
|
||||
:height: 501
|
||||
:created_at: 2022-12-11 16:44:40.766136129 +02:00
|
||||
:size: 40
|
||||
:id: 25
|
||||
- :name: residency_accomodation
|
||||
:type: webp
|
||||
:width: 1740
|
||||
:height: 992
|
||||
:created_at: 2022-12-11 16:44:40.767136120 +02:00
|
||||
:size: 129
|
||||
:id: 26
|
||||
- :name: retreats
|
||||
:type: jpg
|
||||
:width: 1044
|
||||
:height: 500
|
||||
:created_at: 2022-12-11 16:44:40.767136120 +02:00
|
||||
:size: 245
|
||||
:id: 27
|
||||
- :name: service_boutique
|
||||
:type: webp
|
||||
:width: 1000
|
||||
:height: 536
|
||||
:created_at: 2022-12-11 16:44:40.769136102 +02:00
|
||||
:size: 137
|
||||
:id: 28
|
||||
- :name: service_cafe
|
||||
:type: webp
|
||||
:width: 1120
|
||||
:height: 600
|
||||
:created_at: 2022-12-11 16:44:40.769136102 +02:00
|
||||
:size: 81
|
||||
:id: 29
|
||||
- :name: service_treatments
|
||||
:type: webp
|
||||
:width: 592
|
||||
:height: 317
|
||||
:created_at: 2022-12-11 16:44:40.769136102 +02:00
|
||||
:size: 31
|
||||
:id: 30
|
||||
- :name: shared_wide
|
||||
:type: webp
|
||||
:width: 3840
|
||||
:height: 1020
|
||||
:created_at: 2022-12-11 16:44:40.769136102 +02:00
|
||||
:size: 245
|
||||
:id: 31
|
||||
- :name: single_office_small
|
||||
:type: webp
|
||||
:width: 602
|
||||
:height: 580
|
||||
:created_at: 2022-12-11 16:44:40.770136094 +02:00
|
||||
:size: 57
|
||||
:id: 32
|
||||
- :name: sky_conference
|
||||
:type: webp
|
||||
:width: 560
|
||||
:height: 300
|
||||
:created_at: 2022-12-11 16:44:40.770136094 +02:00
|
||||
:size: 146
|
||||
:id: 33
|
||||
- :name: sowing_wide
|
||||
:type: webp
|
||||
:width: 780
|
||||
:height: 262
|
||||
:created_at: 2022-12-11 16:44:40.771136085 +02:00
|
||||
:size: 28
|
||||
:id: 34
|
||||
- :name: standard
|
||||
:type: webp
|
||||
:width: 1730
|
||||
:height: 1032
|
||||
:created_at: 2022-12-07 16:04:22.142547509 +02:00
|
||||
:size: 136
|
||||
:id: 35
|
||||
- :name: studio_kitchen
|
||||
:type: webp
|
||||
:width: 560
|
||||
:height: 300
|
||||
:created_at: 2022-12-11 16:44:40.771136085 +02:00
|
||||
:size: 19
|
||||
:id: 36
|
||||
- :name: studios
|
||||
:type: jpg
|
||||
:width: 1740
|
||||
:height: 794
|
||||
:created_at: 2022-12-11 16:44:40.771136085 +02:00
|
||||
:size: 372
|
||||
:id: 37
|
||||
- :name: suites_wide
|
||||
:type: webp
|
||||
:width: 3840
|
||||
:height: 1064
|
||||
:created_at: 2022-12-11 16:44:40.772136076 +02:00
|
||||
:size: 403
|
||||
:id: 38
|
||||
- :name: the_hall
|
||||
:type: webp
|
||||
:width: 560
|
||||
:height: 300
|
||||
:created_at: 2022-12-11 16:44:40.773136067 +02:00
|
||||
:size: 19
|
||||
:id: 39
|
||||
- :name: wet_space_room
|
||||
:type: webp
|
||||
:width: 865
|
||||
:height: 501
|
||||
:created_at: 2022-12-11 16:44:40.773136067 +02:00
|
||||
:size: 29
|
||||
:id: 40
|
||||
- :name: woodworking_wide
|
||||
:type: webp
|
||||
:width: 1279
|
||||
:height: 415
|
||||
:created_at: 2022-12-11 16:44:40.773136067 +02:00
|
||||
:size: 102
|
||||
:id: 41
|
Loading…
Reference in New Issue
Block a user