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

add mount_uploaders methods #16

Open
wants to merge 1 commit into
base: master
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
66 changes: 66 additions & 0 deletions lib/carrierwave/neo4j.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,72 @@ def _set_uploaders_nil
end
RUBY
end

def mount_uploaders(column, uploader = nil, options = {}, &block)
super

serialize column, ::CarrierWave::Uploader::Base

include CarrierWave::Validations::ActiveModel

validates_integrity_of column if uploader_option(column.to_sym, :validate_integrity)
validates_processing_of column if uploader_option(column.to_sym, :validate_processing)

after_save :"store_#{column}!"
before_save :"write_#{column}_identifier"
before_destroy :"clear_#{column}"
after_destroy :"remove_#{column}!"

class_eval <<-RUBY, __FILE__, __LINE__+1
def #{column}=(new_files)
column = _mounter(:#{column}).serialization_column
send(:attribute_will_change!, :#{column})
super
end

def remote_#{column}_urls=(url)
column = _mounter(:#{column}).serialization_column
send(:attribute_will_change!, :#{column})
super
end

def clear_#{column}
write_uploader(_mounter(:#{column}).serialization_column, nil)
end

def read_uploader(name)
send(:attribute, name.to_s)
end

def write_uploader(name, value)
send(:attribute=, name.to_s, value)
end

def reload_from_database
if reloaded = self.class.load_entity(neo_id)
send(:attributes=, reloaded.attributes.reject{ |k,v| v.is_a?(::CarrierWave::Uploader::Base) })
end
reloaded
end

# carrierwave keeps a instance variable of @uploaders, cached at init time
# but at init time, the value of the column is not yet available
# so after init, the empty @uploaders cache must be invalidated
# it will reinitialized with the processed column value on first access
unless method_defined?(:_set_uploaders_nil)

after_initialize :_set_uploaders_nil

def _set_uploaders_nil
if @_mounters
@_mounters.each do |_, mounter|
mounter.instance_variable_set(:@uploaders, nil)
end
end
end
end
RUBY
end
end

end
Expand Down
6 changes: 5 additions & 1 deletion lib/carrierwave/neo4j/uploader_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ def convert_type
end

def to_db(value)
value.identifier
if value.is_a? Array
value.map(&:identifier)
else
value.identifier
end
end

def to_ruby(value)
Expand Down