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

Update for activegraph #20

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
73 changes: 73 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: Test

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

jobs:
test:
runs-on: ubuntu-18.04
strategy:
matrix:
ruby: [ 2.5, 3.0, jruby-head ]
neo4j: [ 3.5.26, 4.0.11 ]
driver: [ ffi ]
include:
- ruby: jruby-head
neo4j: 4.0.11
driver: java
- ruby: 2.5
neo4j: 3.4.18
driver: ffi
java-version: 8
active-model-version: 5.2.3
env:
NEO4J_EDITION_FLAG: -e
NEO4J_VERSION: ${{ matrix.neo4j }}
driver: ${{ matrix.driver }}
ACTIVE_MODEL_VERSION: ${{ matrix.active-model-version }}
JRUBY_OPTS: --debug -J-Xmx1280m -Xcompile.invokedynamic=false -J-XX:+TieredCompilation -J-XX:TieredStopAtLevel=1 -J-noverify -Xcompile.mode=OFF
steps:
- uses: actions/checkout@v2

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.8.x'
- uses: BSFishy/pip-action@v1
with:
packages: git+https://github.com/klobuczek/[email protected]#egg=boltkit

- name: Set up Java
uses: actions/setup-java@v1
with:
java-version: ${{ matrix.java-version || 11 }}

- name: Setup Neo4j
run: |
neoctrl-install $NEO4J_EDITION_FLAG $NEO4J_VERSION servers
if [ -n "$NEO4J_EDITION_FLAG" ]; then NEO4J_EDITION=enterprise; else NEO4J_EDITION=community; fi
NEO4J_DIR=servers/neo4j-$NEO4J_EDITION-$NEO4J_VERSION
neoctrl-configure $NEO4J_DIR dbms.memory.pagecache.size=600m dbms.memory.heap.max_size=600m dbms.memory.heap.initial_size=600m dbms.directories.import= dbms.connectors.default_listen_address=:: dbms.security.auth_enabled=false
neoctrl-start $NEO4J_DIR

- name: Setup Seabolt
if: matrix.driver != 'java'
run: |
wget https://github.com/neo4j-drivers/seabolt/releases/download/v1.7.4/seabolt-1.7.4-Linux-ubuntu-18.04.deb
sudo dpkg -i seabolt-1.7.4-Linux-ubuntu-18.04.deb

- name: Install dependencies
run: bundle update
- name: Run tests
run: bundle exec rspec
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## CarrierWave for Neo4j
## CarrierWave for ActiveGraph

This gem adds support for Neo4j 3.0+ (neo4j.rb 9.6.0+) to CarrierWave 2.1.0+, see the CarrierWave documentation for more detailed usage instructions.

Expand All @@ -7,10 +7,10 @@ This gem adds support for Neo4j 3.0+ (neo4j.rb 9.6.0+) to CarrierWave 2.1.0+, se
Add to your Gemfile:

```ruby
gem 'carrierwave-neo4j', '~> 3.0', require: 'carrierwave/neo4j'
gem 'carrierwave-neo4j', '~> 3.0', require: 'carrierwave/active_graph'
```

You can see example usage in `spec/neo4j_realistic_spec.rb` but in brief, you can use it like this:
You can see example usage in `spec/active_graph_realistic_spec.rb` but in brief, you can use it like this:

```ruby
class AttachmentUploader < CarrierWave::Uploader::Base
Expand All @@ -22,7 +22,7 @@ class AttachmentUploader < CarrierWave::Uploader::Base
end

class Asset
include Neo4j::ActiveNode
include ActiveGraph::Node

property :attachment, type: String
mount_uploader :attachment, AttachmentUploader
Expand All @@ -47,7 +47,7 @@ brew install imagemagick

```sh
bundle install
rake neo4j:install[community-latest,test]
rake neo4j:install[community-4.0.11,test]
rake neo4j:start[test]
rake spec
```
Expand Down
9 changes: 5 additions & 4 deletions carrierwave-neo4j.gemspec
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "carrierwave/neo4j/version"
require "carrierwave/active_graph/version"

Gem::Specification.new do |s|
s.name = "carrierwave-neo4j"
s.version = CarrierWave::Neo4j::VERSION
s.name = "carrierwave-activegraph"
s.version = CarrierWave::ActiveGraph::VERSION
s.authors = ["Rodrigo Navarro"]
s.email = ["[email protected]"]
s.homepage = "https://github.com/neo4jrb/carrierwave-neo4j"
Expand All @@ -17,8 +17,9 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]

s.add_dependency("activesupport", ">= 6.0" )
s.add_dependency("neo4j", "~> 9.6.0")
s.add_dependency("activegraph", ">= 10.0.0")
s.add_dependency("carrierwave", ">= 2.1")
s.add_development_dependency("neo4j-#{ENV['driver'] == 'java' ? 'java' : 'ruby'}-driver", '~> 1.7.4')
s.add_development_dependency("rspec", "~> 3.0")
s.add_development_dependency("rspec-its")
s.add_development_dependency("webmock")
Expand Down
15 changes: 8 additions & 7 deletions lib/carrierwave/neo4j.rb → lib/carrierwave/active_graph.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require "carrierwave/neo4j/version"
require "neo4j"
require "carrierwave/active_graph/version"
require "active_graph"
require "carrierwave"
require "carrierwave/validations/active_model"
require "carrierwave/neo4j/uploader_converter"
require "carrierwave/active_graph/uploader_converter"
require "active_support/concern"

######
Expand All @@ -14,7 +14,7 @@
# ...which is mixed into Model classes.
######
module CarrierWave
module Neo4j
module ActiveGraph

# this class methods junk is necessary because ActiveNode is implemented as
# a model instead of a class for god-knows-what-reason.
Expand Down Expand Up @@ -85,7 +85,8 @@ def mount_base(column, uploader=nil, options={}, &block)
# 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
after_initialize :_set_uploaders_nil
# TODO: This currently break things when initializing a model with #new and parameters. Do we need it?
#after_initialize :_set_uploaders_nil

before_save :"write_#{column}_identifier"
after_save :"store_#{column}!"
Expand Down Expand Up @@ -159,7 +160,7 @@ def _force_uploaders_reload

# okay, this actually works:
def force_retrieve_#{column}
send(:#{column}).send(:retrieve_from_store!, #{column}_identifier)
send(:#{column}).send(:retrieve_from_store!, #{column}_identifier) if #{column}_identifier
end

# these produce an infinite loop, so... don't reintroduce them:
Expand Down Expand Up @@ -208,4 +209,4 @@ def store_previous_changes_for_#{column}
end
end

Neo4j::ActiveNode.include CarrierWave::Neo4j
ActiveGraph::Node.include CarrierWave::ActiveGraph
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module CarrierWave::Neo4j
module CarrierWave::ActiveGraph
module TypeConverters
class UploaderConverter
class << self
Expand All @@ -23,14 +23,14 @@ def to_ruby(value)
end

def register_converter(mod)
mod::TypeConverters.send :include, CarrierWave::Neo4j::TypeConverters
mod::TypeConverters.send :include, CarrierWave::ActiveGraph::TypeConverters
end

major = Neo4j::VERSION.split('.').first.to_i
major = ActiveGraph::VERSION.split('.').first.to_i

case major
when 1..2 then fail('Unsupported version of Neo4j gem. Please update it.')
when 3 then register_converter(Neo4j)
when 4 then register_converter(Neo4j::Shared)
when 5..Float::INFINITY then Neo4j::Shared::TypeConverters.register_converter(CarrierWave::Neo4j::TypeConverters::UploaderConverter)
when 5..Float::INFINITY then ActiveGraph::Shared::TypeConverters.register_converter(CarrierWave::ActiveGraph::TypeConverters::UploaderConverter)
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module CarrierWave
module Neo4j
module ActiveGraph
VERSION = '3.0.0'
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

def extension_whitelist
def extension_allowlist
%w(jpg jpeg gif png)
end
end

class Book
include Neo4j::ActiveNode
include ActiveGraph::Node
property :cover, type: String
mount_uploader :cover, RealisticUploader
end

describe CarrierWave::Neo4j do
describe CarrierWave::ActiveGraph do
after do
Book.destroy_all
end
Expand Down
47 changes: 42 additions & 5 deletions spec/neo4j_sanity_spec.rb → spec/active_graph_sanity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ def reset_class
Object.send(:remove_const, "User") rescue nil
Object.const_set("User", Class.new())
User.class_eval do
include Neo4j::ActiveNode
include ActiveGraph::Node
property :image, type: String
mount_uploader :image, DefaultUploader
end
User
end

describe CarrierWave::Neo4j do
describe CarrierWave::ActiveGraph do
before do
reset_class
@user = User.new
Expand Down Expand Up @@ -68,6 +68,18 @@ def reset_class
end

describe "#image" do
it "should return blank uploader when nothing has been assigned" do
expect(@user.image).to be_blank
end

it "should return blank uploader when an blank uploader has been assigned" do
@user[:image] = DefaultUploader.new
@user.save!
@user.reload
other = User.find(@user.id)
expect(@user.image).to be_blank
expect(other.image).to be_blank
end
end

describe "#save" do
Expand All @@ -85,6 +97,30 @@ def reset_class
expect(@user[:image].identifier).to eq('ong.jpg')
expect(@user.image_identifier).to eq('ong.jpg')
end

it "should assign the filename to the database even if uploader was previous nil" do
expect(@user.save).to be_truthy
expect(@user.image).to be_blank
image = File.open(file_path("ong.jpg"))
expect(@user.update(image: image)).to be_truthy
@user.reload
expect(@user[:image]).not_to eq('ong.jpg')
expect(@user[:image].identifier).to eq('ong.jpg')
expect(@user.image_identifier).to eq('ong.jpg')
end

it "should assign the filename to the database when using #new even if uploader was previous nil" do
expect(@user.save).to be_truthy
expect(@user.image).to be_blank
image = File.open(file_path("ong.jpg"))
user = User.new(image: image)
expect(user.save).to be_truthy
user.reload
expect(user[:image]).not_to eq('ong.jpg')
expect(user[:image].identifier).to eq('ong.jpg')
expect(user.image_identifier).to eq('ong.jpg')
end

end

describe "#update" do
Expand All @@ -101,16 +137,15 @@ def reset_class
expect(@user.reload.image).to be_blank
end

it "does not respect `update_column`" do
it "does respect `update_column` only after find" do
@user.image = File.open(file_path("ong.jpg"))
@user.save!

# ActiveRecord would respect `update_column`
User.find(@user.id).update_column(:image, nil)

expect(@user.reload.image).to be_present
other = User.find(@user.id)
expect(other.image).to be_present
expect(other.image).to be_blank
end
end

Expand All @@ -119,6 +154,8 @@ def reset_class

describe "#remote_image_url=" do
before do
allow_any_instance_of(CarrierWave::Downloader::Base).to receive(:skip_ssrf_protection?).
and_return(true)
stub_request(:get, "www.example.com/test.jpg").to_return(body: File.read(file_path("ong.jpg")))
end

Expand Down
6 changes: 3 additions & 3 deletions spec/neo4j_spec.rb → spec/active_graph_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ def reset_class(uploader = DefaultUploader)
end

class User
include Neo4j::ActiveNode
include ActiveGraph::Node
property :image, type: String
end

class DefaultUploader < CarrierWave::Uploader::Base ; end

class PngUploader < CarrierWave::Uploader::Base
def extension_whitelist
def extension_allowlist
%w(png)
end
end
Expand All @@ -34,7 +34,7 @@ def download!(file, headers = {})
end
end

describe CarrierWave::Neo4j do
describe CarrierWave::ActiveGraph do
let(:user_class) { reset_class }
let(:user_class_png) { reset_class(PngUploader) }
let(:user_class_error) { reset_class(ProcessingErrorUploader) }
Expand Down
8 changes: 4 additions & 4 deletions spec/helpers/database_cleaner.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

require "neo4j"
require "active_graph"

class DatabaseCleaner
include Neo4j::Migrations::Helpers
include ActiveGraph::Migrations::Helpers

def self.clean
DatabaseCleaner.new.clean_db
Expand All @@ -11,9 +11,9 @@ def self.clean
def self.avoid_validation
# migrations and db cleanup have to happen outside of validations
# or they never succeed
Neo4j::Migrations.currently_running_migrations = true
ActiveGraph::Migrations.currently_running_migrations = true
yield
Neo4j::Migrations.currently_running_migrations = false
ActiveGraph::Migrations.currently_running_migrations = false
end

def clean_db
Expand Down
4 changes: 2 additions & 2 deletions spec/helpers/fake_book_migration.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

require "neo4j"
require "active_graph"

class FakeBookMigration < Neo4j::Migrations::Base
class FakeBookMigration < ActiveGraph::Migrations::Base

def self.create
FakeBookMigration.new(:fake_book_migration)
Expand Down
Loading