Skip to content

Commit

Permalink
fixed saving and reloading after recent changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dpisarewski committed Dec 12, 2014
1 parent adca4dd commit ab1b68c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
22 changes: 22 additions & 0 deletions README.md
@@ -0,0 +1,22 @@
## CarrierWave for Neo4j

This gem adds support for Neo4j to CarrierWave, see the CarrierWave documentation for more detailed usage instructions.

### Installation Instructions

Add to your Gemfile:

```ruby
gem 'carrierwave-neo4j', require: 'carrierwave/neo4j', github: 'dpisarewski/carrierwave-neo4j'
```
Use it like this:

```ruby
class Asset
include Neo4j::ActiveNode
extend CarrierWave::Neo4j

property :attachment, type: String
mount_uploader :attachment, AttachmentUploader
end
```
12 changes: 10 additions & 2 deletions lib/carrierwave/neo4j.rb
Expand Up @@ -2,6 +2,7 @@
require "neo4j"
require "carrierwave"
require "carrierwave/validations/active_model"
require "carrierwave/neo4j/uploader_converter"

module CarrierWave
module Neo4j
Expand All @@ -11,10 +12,10 @@ module Neo4j
# See +CarrierWave::Mount#mount_uploader+ for documentation
#
def mount_uploader(column, uploader = nil, options = {}, &block)
property column

super

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

include CarrierWave::Validations::ActiveModel

validates_integrity_of column if uploader_option(column.to_sym, :validate_integrity)
Expand Down Expand Up @@ -43,6 +44,13 @@ def read_uploader(name)
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
RUBY
end
end
Expand Down
19 changes: 19 additions & 0 deletions lib/carrierwave/neo4j/uploader_converter.rb
@@ -0,0 +1,19 @@
module Neo4j::Shared
module TypeConverters
class UploaderConverter
class << self
def convert_type
::CarrierWave::Uploader::Base
end

def to_db(value)
value.identifier
end

def to_ruby(value)
value
end
end
end
end
end

0 comments on commit ab1b68c

Please sign in to comment.