Skip to content

Commit

Permalink
Add attempt to a test case for active storage
Browse files Browse the repository at this point in the history
- I am having a hard time on where to locate the method and have access
to the method name and the instance so I can do something like:
```ruby
file_loader = FileLoader.new()
instance.send(method_name).attach(
  io: file_loader.io, filename: file_loader.filename
)
```
  • Loading branch information
aledustet committed Aug 2, 2019
1 parent bb18150 commit d5e37f8
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 25 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
source "https://rubygems.org"

gemspec name: "factory_bot"

gem 'pry'
gem 'activestorage'
43 changes: 43 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,29 @@ PATH
GEM
remote: https://rubygems.org/
specs:
actionpack (5.2.3)
actionview (= 5.2.3)
activesupport (= 5.2.3)
rack (~> 2.0)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (5.2.3)
activesupport (= 5.2.3)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.3)
activemodel (5.2.3)
activesupport (= 5.2.3)
activerecord (5.2.3)
activemodel (= 5.2.3)
activesupport (= 5.2.3)
arel (>= 9.0)
activestorage (5.2.3)
actionpack (= 5.2.3)
activerecord (= 5.2.3)
marcel (~> 0.3.1)
activesupport (5.2.3)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
Expand All @@ -35,8 +52,10 @@ GEM
builder (3.2.3)
childprocess (1.0.1)
rake (< 13.0)
coderay (1.1.2)
concurrent-ruby (1.1.5)
contracts (0.16.0)
crass (1.0.4)
cucumber (3.1.2)
builder (>= 2.1.2)
cucumber-core (~> 3.2.0)
Expand All @@ -55,18 +74,40 @@ GEM
cucumber-wire (0.0.1)
diff-lcs (1.3)
docile (1.3.2)
erubi (1.8.0)
ffi (1.11.1)
gherkin (5.1.0)
i18n (1.6.0)
concurrent-ruby (~> 1.0)
json (2.2.0)
loofah (2.2.3)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
marcel (0.3.3)
mimemagic (~> 0.3.2)
method_source (0.9.2)
mimemagic (0.3.3)
mini_portile2 (2.4.0)
minitest (5.11.3)
multi_json (1.13.1)
multi_test (0.1.2)
nokogiri (1.10.3)
mini_portile2 (~> 2.4.0)
parallel (1.17.0)
parser (2.6.3.0)
ast (~> 2.4.0)
powerpack (0.1.2)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
rack (2.0.7)
rack-test (1.1.0)
rack (>= 1.0, < 3)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.0.4)
loofah (~> 2.2, >= 2.2.2)
rainbow (3.0.0)
rake (12.3.2)
rspec (3.8.0)
Expand Down Expand Up @@ -111,10 +152,12 @@ PLATFORMS

DEPENDENCIES
activerecord
activestorage
appraisal
aruba
cucumber
factory_bot!
pry
rake
rspec
rspec-its
Expand Down
14 changes: 0 additions & 14 deletions lib/factory_bot/definition_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,20 +160,6 @@ def association(name, *options)
end
end

# Creates a copy of the file passed to attach to the attribute
#
# Arguments:
# filepath: (String)
# The path for the file that will be loaded. Should be a valid path
# for a file that exists at that location.
#
# Returns:
# An instance of the file loader class pointing to the file at the
# provided path.
def file_fixture(file_path)
FileLoader.new(file_path)
end

def to_create(&block)
@definition.to_create(&block)
end
Expand Down
18 changes: 14 additions & 4 deletions lib/factory_bot/evaluator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ def initialize(build_strategy, overrides = {})
end
end

# Creates a copy of the file passed to attach to the attribute
#
# Arguments:
# filepath: (String)
# The path for the file that will be loaded. Should be a valid path
# for a file that exists at that location.
#
# Returns:
# An instance of the file loader class pointing to the file at the
# provided path.
def file_fixture(file_path)
FileLoader.new(file_path)
end

def association(factory_name, *traits_and_overrides)
overrides = traits_and_overrides.extract_options!
strategy_override = overrides.fetch(:strategy) do
Expand All @@ -37,10 +51,6 @@ def instance=(object_instance)
@instance = object_instance
end

def file_fixture(file_path)
FileLoader.new(file_path)
end

def method_missing(method_name, *args, &block) # rubocop:disable Style/MethodMissing
if @instance.respond_to?(method_name)
@instance.send(method_name, *args, &block)
Expand Down
8 changes: 4 additions & 4 deletions lib/factory_bot/file_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ def initialize(file_path)
from_file_path(file_path)
end

def path
tempfile.path
end

def method_missing(method_name, *args, &block) # rubocop:disable Style/MethodMissing
tempfile.public_send(method_name, *args, &block)
end
Expand All @@ -20,6 +16,10 @@ def respond_to_missing?(method_name, include_private = false)
tempfile.respond_to?(method_name, include_private) || super
end

def io
tempfile.read
end

private

def from_file_path(path)
Expand Down
23 changes: 21 additions & 2 deletions spec/acceptance/file_attributes_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
describe "file attributes" do
class Attachment
attr_accessor :file

def initialize(file)
@file = file
end

def attach(*args); end
end

context "when an attribute uses a file" do
it "assigns an file to the attribute" do
define_model("Post") do
def self.has_one_attached(*args); end
has_one_attached :attachment
attr_accessor :attachment
def attachment=(value)
@attachment = Attachment.new(value)
end
end

filename = File.expand_path("spec/support/text_file.txt")
file_contents = File.read(filename)

Expand All @@ -13,8 +29,11 @@
end
end
post = FactoryBot.build(:post)
expect(post.attachment.original_filename).to eq ::File.basename filename
expect(File.read(post.attachment)).to eq file_contents
puts post.attachment.file.io
expect(post.attachment).to receive(:attach).with(
io: file_contents,
filename: filename,
)
end
end
end
7 changes: 6 additions & 1 deletion spec/support/macros/define_constant.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "active_record"
require "active_storage"

module DefineConstantMacros
def define_class(path, base = Object, &block)
Expand All @@ -7,8 +8,12 @@ def define_class(path, base = Object, &block)
const
end

class ApplicationRecord < ActiveRecord::Base
include ActiveStorage
end

def define_model(name, columns = {}, &block)
model = define_class(name, ActiveRecord::Base, &block)
model = define_class(name, ApplicationRecord, &block)
create_table(model.table_name) do |table|
columns.each do |column_name, type|
table.column column_name, type
Expand Down

0 comments on commit d5e37f8

Please sign in to comment.