Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom field name #33

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ image_tag user.avatar.url(:medium), size: user.avatar.image_size(:medium)
image_tag user.avatar.url(:thumb), size: user.avatar.image_size(:thumb)
```

If you want to use a different column extension you can either set it in an initializer for all models

```ruby
# uses User#avatar_sizes instead of User#avatar_meta
Paperclip::Meta::Attachment.default_meta_data_attribute = :sizes
```

Or you can for specific models by adding a method to the model

```ruby
class User
def meta_data_attribute
:sizes
end
end
```

### Internals

The meta column is simple hash:
Expand Down
29 changes: 24 additions & 5 deletions lib/paperclip-meta/attachment.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
module Paperclip
module Meta
module Attachment
@@default_meta_data_attribute = :meta

def self.default_meta_data_attribute
@@default_meta_data_attribute
end

def self.default_meta_data_attribute=(attrib)
@@default_meta_data_attribute = attrib
end

def self.included(base)
base.send :include, InstanceMethods
base.extend ClassMethods
base.alias_method_chain :save, :meta_data
base.alias_method_chain :post_process_styles, :meta_data
base.alias_method_chain :size, :meta_data
end

module InstanceMethods
def meta_data_attribute
if instance.respond_to?(:meta_data_attribute)
instance.meta_data_attribute
else
Paperclip::Meta::Attachment.default_meta_data_attribute
end
end

def save_with_meta_data
if @queued_for_delete.any? && @queued_for_write.empty?
instance_write(:meta, meta_encode({}))
instance_write(meta_data_attribute, meta_encode({}))
end
save_without_meta_data
end

def post_process_styles_with_meta_data(*styles)
post_process_styles_without_meta_data(*styles)
return unless instance.respond_to?(:"#{name}_meta=")
return unless instance_respond_to?(meta_data_attribute)

meta = populate_meta(@queued_for_write)
return if meta == {}
Expand Down Expand Up @@ -67,8 +86,8 @@ def write_meta(meta)

# Return meta data for given style
def read_meta(style, item)
if instance.respond_to?(:"#{name}_meta") && instance_read(:meta)
if (meta = meta_decode(instance_read(:meta)))
if instance_read(meta_data_attribute)
if (meta = meta_decode(instance_read(meta_data_attribute)))
meta[style] && meta[style][item]
end
end
Expand All @@ -87,7 +106,7 @@ def meta_decode(meta)
# Retain existing meta values that will not be recalculated when
# reprocessing a subset of styles
def merge_existing_meta_hash(meta)
return unless (original_meta = instance.send("#{name}_meta"))
return unless (original_meta = instance_read(meta_data_attribute))
meta.reverse_merge! meta_decode(original_meta)
end
end
Expand Down