Commit 939a9dee by John Doe

added a db_folder_service

parent 30a69ce6
......@@ -19,3 +19,5 @@ vendor/bundle
# Ignore e-macs backup files
*~
.byebug_history
\ No newline at end of file
......@@ -37,6 +37,8 @@ group :development, :test do
gem 'byebug'
gem 'rspec-rails', '~> 3.1'
gem 'rubocop', require: false
gem 'factory_girl_rails'
gem 'faker'
end
group :development, :test do
......
......@@ -55,6 +55,13 @@ GEM
diff-lcs (1.2.5)
erubis (2.7.0)
execjs (2.6.0)
factory_girl (4.7.0)
activesupport (>= 3.0.0)
factory_girl_rails (4.7.0)
factory_girl (~> 4.7.0)
railties (>= 3.0.0)
faker (1.6.3)
i18n (~> 0.5)
ffi (1.9.10)
formatador (0.2.5)
globalid (0.3.6)
......@@ -224,6 +231,8 @@ PLATFORMS
DEPENDENCIES
byebug
coffee-rails (~> 4.1.0)
factory_girl_rails
faker
guard
guard-rspec
guard-rubocop
......
......@@ -26,52 +26,54 @@
notification :terminal_notifier if `uname` =~ /Darwin/
guard :rspec, cmd: "bin/rspec" do
require "guard/rspec/dsl"
dsl = Guard::RSpec::Dsl.new(self)
group :red_green_refactor, halt_on_fail: true do
guard :rspec, cmd: "bin/rspec" do
require "guard/rspec/dsl"
dsl = Guard::RSpec::Dsl.new(self)
# Feel free to open issues for suggestions and improvements
# Feel free to open issues for suggestions and improvements
# RSpec files
rspec = dsl.rspec
watch(rspec.spec_helper) { rspec.spec_dir }
watch(rspec.spec_support) { rspec.spec_dir }
watch(rspec.spec_files)
# RSpec files
rspec = dsl.rspec
watch(rspec.spec_helper) { rspec.spec_dir }
watch(rspec.spec_support) { rspec.spec_dir }
watch(rspec.spec_files)
# Ruby files
ruby = dsl.ruby
dsl.watch_spec_files_for(ruby.lib_files)
# Ruby files
ruby = dsl.ruby
dsl.watch_spec_files_for(ruby.lib_files)
# Rails files
rails = dsl.rails(view_extensions: %w(erb haml slim))
dsl.watch_spec_files_for(rails.app_files)
dsl.watch_spec_files_for(rails.views)
# Rails files
rails = dsl.rails(view_extensions: %w(erb haml slim))
dsl.watch_spec_files_for(rails.app_files)
dsl.watch_spec_files_for(rails.views)
watch(rails.controllers) do |m|
[
rspec.spec.("routing/#{m[1]}_routing"),
rspec.spec.("controllers/#{m[1]}_controller"),
rspec.spec.("acceptance/#{m[1]}")
]
end
watch(rails.controllers) do |m|
[
rspec.spec.("routing/#{m[1]}_routing"),
rspec.spec.("controllers/#{m[1]}_controller"),
rspec.spec.("acceptance/#{m[1]}")
]
end
# Rails config changes
watch(rails.spec_helper) { rspec.spec_dir }
watch(rails.routes) { "#{rspec.spec_dir}/routing" }
watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
# Rails config changes
watch(rails.spec_helper) { rspec.spec_dir }
watch(rails.routes) { "#{rspec.spec_dir}/routing" }
watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
# Capybara features specs
watch(rails.view_dirs) { |m| rspec.spec.("features/#{m[1]}") }
watch(rails.layouts) { |m| rspec.spec.("features/#{m[1]}") }
# Capybara features specs
watch(rails.view_dirs) { |m| rspec.spec.("features/#{m[1]}") }
watch(rails.layouts) { |m| rspec.spec.("features/#{m[1]}") }
# Turnip features and steps
watch(%r{^spec/acceptance/(.+)\.feature$})
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
# Turnip features and steps
watch(%r{^spec/acceptance/(.+)\.feature$})
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
end
end
end
guard :rubocop do
watch(%r{.+\.rb$})
watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
guard :rubocop do
watch(%r{.+\.rb$})
watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
end
end
......@@ -3,7 +3,7 @@
# A file in the database, contains one or more Streams
class DbFile < ActiveRecord::Base
belongs_to :db_folder
has_many :streams
has_many :db_streams, dependent: :destroy
def remove(db_service:)
db_service.remove_file(path)
......
......@@ -3,6 +3,15 @@
# a folder in the database, may contain one or more DbFiles as files
# and one or more DbFolders as subfolders
class DbFolder < ActiveRecord::Base
belongs_to :parent, class_name: 'DbFolder'
has_many :subfolders
has_many :files
has_many :db_files
def insert_file(file:)
# add the file to this folder
file.db_folder = self
# verify that the file can be here
return false unless file.valid?
true
end
end
# frozen_string_literal: true
# Service class for DbFolders
class DbFolderService
attr_accessor :error_msg
def initialize(db_service:, db_builder:)
@db_service = db_service
@db_builder = db_builder
end
def insert_file(folder:, file:)
@error_msg = ''
return false unless __put_file_in_folder(file: file, folder: folder)
return false unless __make_path_for_file(file: file, folder: folder)
return false unless __create_file_on_db(file: file)
file.save!
end
def __put_file_in_folder(file:, folder:)
return true if folder.insert_file(file: file)
@error_msg = "could not add file to folder #{folder.name}"
false
end
def __make_path_for_file(file:, folder:)
file.path = @db_builder.build_path(folder_path: folder.path,
file_name: file.name)
true
end
def __create_file_on_db(file:)
return true if @db_service.create_file(file)
@error_msg = "from db_service: #{db_service.error_msg}"
file.path = ''
file.folder = nil
false
end
end
class AddParentToDbFolders < ActiveRecord::Migration
def change
add_column :db_folders, :parent_id, :integer
end
end
class AddPathToFolder < ActiveRecord::Migration
def change
add_column :db_folders, :path, :string
end
end
......@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160516024809) do
ActiveRecord::Schema.define(version: 20160517015954) do
create_table "db_files", force: :cascade do |t|
t.string "name"
......@@ -27,6 +27,8 @@ ActiveRecord::Schema.define(version: 20160516024809) do
t.string "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "parent_id"
t.string "path"
end
create_table "db_streams", force: :cascade do |t|
......
# frozen_string_literal: true
# generic DbStream
FactoryGirl.define do
factory :db_file do
name { Faker::Lorem.word }
end
end
# frozen_string_literal: true
# generic DbFolder
FactoryGirl.define do
factory :db_folder do
name { Faker::Lorem.word }
end
end
# frozen_string_literal: true
# generic DbStream
FactoryGirl.define do
factory :db_stream do
name { Faker::Lorem.word }
end
end
......@@ -6,16 +6,24 @@ RSpec.describe 'DbFile' do
let(:db_file) { DbFile.new }
specify { expect(db_file).to respond_to(:name) }
specify { expect(db_file).to respond_to(:description) }
specify { expect(db_file).to respond_to(:streams) }
specify { expect(db_file).to respond_to(:db_streams) }
end
describe 'remove' do
let(:db_file) { DbFile.create }
let(:db_streams) { FactoryGirl.build_list(:db_stream, 5) }
let(:db_file) { FactoryGirl.create(:db_file) }
let(:db_service) { double(remove_file: true) }
it 'destroys itself' do
db_file.remove(db_service: db_service)
expect(db_file).to be_destroyed
end
it 'destroys its db_streams' do
db_file.db_streams << db_streams
db_file.remove(db_service: db_service)
db_streams.each do |stream|
expect(stream).to be_destroyed
end
end
it 'removes itself from the remote system using DbService' do
db_file.remove(db_service: db_service)
expect(db_service).to have_received(:remove_file)
......
......@@ -6,7 +6,18 @@ RSpec.describe 'DbFolder' do
let(:db_folder) { DbFolder.new }
specify { expect(db_folder).to respond_to(:name) }
specify { expect(db_folder).to respond_to(:description) }
specify { expect(db_folder).to respond_to(:parent) }
specify { expect(db_folder).to respond_to(:subfolders) }
specify { expect(db_folder).to respond_to(:files) }
specify { expect(db_folder).to respond_to(:db_files) }
end
describe 'insert_file' do
let(:db_folder) { FactoryGirl.create(:db_folder) }
let(:new_file) { FactoryGirl.create(:db_file) }
it 'adds the file to subfolders' do
db_folder.insert_file(file: new_file)
expect(new_file.db_folder).to eq(db_folder)
end
end
end
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'DbFileService' do
describe 'insert_file' do
# mock the DbService and DbBuilder
let(:db_service) { double(create_file: true) }
let(:db_builder) { double(build_path: '/test/file') }
# a file to insert
let(:new_file) { FactoryGirl.build_stubbed(:db_file) }
# a folder to insert it in
let(:parent_folder) { FactoryGirl.build_stubbed(:db_folder) }
it 'adds the given file to the folder' do
db_folder_service = DbFolderService.new(db_service: db_service,
db_builder: db_builder)
db_folder_service.insert_file(folder: parent_folder, file: new_file)
expect(new_file.db_folder).to eq(parent_folder)
end
it 'does not add the file if the db_service fails'
end
end
# frozen_string_literal: true
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment