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

Refactor: Pass Project object instead of project_id when searching tasks... #46

Open
wants to merge 2 commits into
base: 2.0
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
2 changes: 1 addition & 1 deletion lib/taskmapper/entities/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def validate
end

def tasks
factory.tasks.where :project_id => self.id
factory.tasks.where :project => self
end

def create_task(attrs)
Expand Down
19 changes: 18 additions & 1 deletion lib/taskmapper/entities/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ class Task
attr_accessor :title,
:description,
:requestor,
:assignee,
:assignee,
:project,
:project_id,
:priority,
:status,
:factory

protected :requestor=,
:project=,
:project_id=,
:factory=

Expand All @@ -25,9 +27,18 @@ def initialize(attrs)
self.priority = attrs[:priority]
self.status = attrs[:status]
self.project_id = attrs[:project_id]
self.project = attrs[:project]
self.factory = attrs[:factory]
validate
end

def project_id
@project_id ||= project.id
end

def project
@project ||= factory.projects.find project_id
end

def save
factory.tasks.update self
Expand All @@ -40,6 +51,12 @@ def delete
def validate
validate_presence_of :title
validate_presence_of :requestor
validate_project
end

def validate_project
raise TaskMapper::Exceptions::TaskMapperException
.new("Must provider a project_id or a project object") if @project_id.nil? and @project.nil?
end

def create_comment(attrs)
Expand Down
10 changes: 4 additions & 6 deletions lib/taskmapper/repositories/entity_repository.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module TaskMapper
module Repositories
class EntityRepository < Repository
def search(criteria = {})
super(criteria).map { |attributes| new_entity(attributes) }
def search(filter = {})
super(filter).map { |attributes| new_entity(attributes) }
end

def create(attributes)
Expand All @@ -11,9 +11,7 @@ def create(attributes)

def update(entity)
updated = super entity
entity.instance_eval do
updated_at = Time.now
end if updated
entity.instance_eval { updated_at = Time.now } if updated
updated
end

Expand All @@ -40,7 +38,7 @@ def <<(entity)
end

def new_entity(attrs)
factory.entity(entity_class, attrs.merge(criteria))
factory.entity(entity_class, attrs.merge(filter))
end

def new_entity_or_nil(attrs)
Expand Down
16 changes: 8 additions & 8 deletions lib/taskmapper/repositories/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ module Repositories
class Repository
include Enumerable

attr_accessor :factory, :criteria, :entity_class
attr_accessor :factory, :filter, :entity_class

protected :factory=, :criteria=, :entity_class=
protected :factory=, :filter=, :entity_class=

def initialize(factory, entity_class, criteria = {})
def initialize(factory, entity_class, filter = {})
self.factory = factory
self.entity_class = entity_class
self.criteria = criteria
self.filter = filter
end

def provider
factory.provider self.entity_class
end

def each(&block)
search(criteria).each &block
search(filter).each &block
end

def search(criteria = {})
provider.search(criteria)
provider.search(criteria.merge filter)
end

def update(attributes)
Expand All @@ -42,7 +42,7 @@ def find_by_id(id)
end

def find_by_attributes(attrs)
provider.find_by_attributes(attrs.merge criteria)
provider.find_by_attributes(attrs.merge filter)
end

def find_by_attribute(attribute, value)
Expand All @@ -58,7 +58,7 @@ def [](index)
end

def where(criteria = {})
self.class.new factory, self.criteria.merge(criteria)
self.class.new factory, filter.merge(criteria)
end

# Dynamic finder
Expand Down
17 changes: 12 additions & 5 deletions spec/fake_providers/in_memory_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ module Tasks
include InMemoryProvider
include Finders

def search(criteria)
project = criteria.delete :project
criteria[:project_id] = project.id if project
super criteria
end

def find_by_attributes(attrs)
attrs.delete :project
super attrs
end

def supported_operations
[:create, :search, :find]
end
Expand All @@ -82,12 +93,8 @@ module TaskComments
include InMemoryProvider
include Finders

def create(comment)
super comment
end

def search(criteria)
task = criteria.delete(:task)
criteria.delete(:task)
super criteria
end

Expand Down