forked from gzigzigzeo/carrierwave-meta
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Approach to save all metadata in single column
- Loading branch information
1 parent
83c7ddb
commit 2d9d1a5
Showing
8 changed files
with
116 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module CarrierWave | ||
module Meta | ||
module ActiveRecord | ||
ALLOWED = %w(width height md5sum image_size file_size content_type) | ||
|
||
def carrierwave_meta_composed(single_attribute, *args) | ||
defined_attrs = args.map do |arg| | ||
name, to_define = if arg.is_a?(Symbol) | ||
[arg, ALLOWED] | ||
elsif arg.is_a?(Hash) | ||
[arg.keys.first, arg.values.first] | ||
end | ||
|
||
to_define.map do |attr| | ||
delegate :"#{name}_#{attr}", to: single_attribute, allow_nil: true | ||
delegate :"#{name}_#{attr}=", to: single_attribute, allow_nil: true | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require 'spec_helper' | ||
|
||
describe CarrierWave::Meta::ActiveRecord do | ||
let(:model) { TestComposedModel.new } | ||
let(:model_with_image) do | ||
model.tap do |m| | ||
m.image = File.open('spec/fixtures/big.jpg') | ||
m.save! | ||
end | ||
end | ||
|
||
it "model's virtual meta attributes must exists" do | ||
subject::ALLOWED.each do |name| | ||
model.should respond_to(:"image_#{name}") | ||
end | ||
end | ||
|
||
it "must assign model's virtual attributes and save meta column" do | ||
subject::ALLOWED.each do |name| | ||
model_with_image.send(:"image_#{name}").should_not be_blank | ||
end | ||
|
||
model_with_image.image_version_width.should_not be_blank | ||
model_with_image.image_version_height.should_not be_blank | ||
|
||
model_with_image.reload | ||
|
||
model_with_image.image_version_width.should_not be_blank | ||
model_with_image.image_version_height.should_not be_blank | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,10 @@ | ||
ActiveRecord::Schema.define :version => 0 do | ||
create_table "test_models", :force => true do |t| | ||
t.string :image | ||
|
||
t.integer :image_width | ||
t.integer :image_height | ||
t.string :image_content_type | ||
t.integer :image_file_size | ||
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:") | ||
|
||
ActiveRecord::Schema.define :version => 1 do | ||
|
||
t.integer :image_version_width | ||
t.integer :image_version_height | ||
t.integer :image_version_file_size | ||
t.string :image_version_content_type | ||
create_table "test_composed_models", :force => true do |t| | ||
t.string :image | ||
t.text :image_meta | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class TestComposedModel < ActiveRecord::Base | ||
extend CarrierWave::Meta::ActiveRecord | ||
|
||
mount_uploader :image, TestUploader | ||
serialize :image_meta, OpenStruct | ||
carrierwave_meta_composed :image_meta, :image, image_version: [:width, :height] | ||
end |