Skip to content

Commit

Permalink
Merge pull request #382 from DylanGuedes/has_owner_concern
Browse files Browse the repository at this point in the history
Refactor a few modules to use has_owner concern
  • Loading branch information
rafamanzo authored Oct 3, 2016
2 parents 42e5d46 + 929ea2d commit a5f2d20
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 52 deletions.
21 changes: 21 additions & 0 deletions app/models/concerns/has_owner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'active_support/concern'

module HasOwner
extend ActiveSupport::Concern

class_methods do
def public_or_owned_by_user(user = nil)
class_name = name+"Attributes"
collection = class_name.constantize.where(public: true)
collection += class_name.constantize.where(user_id: user.id, public: false) if user

collection.map do |item|
begin
self.find(item.send(name.underscore+"_id"))
rescue Likeno::Errors::RecordNotFound
nil
end
end.compact
end
end
end
15 changes: 2 additions & 13 deletions app/models/kalibro_configuration.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
class KalibroConfiguration < KalibroClient::Entities::Configurations::KalibroConfiguration
include KalibroRecord
attr_writer :attributes

def self.public_or_owned_by_user(user=nil)
kalibro_configuration_attributes = KalibroConfigurationAttributes.where(public: true)
kalibro_configuration_attributes += KalibroConfigurationAttributes.where(user_id: user.id, public: false) if user
include HasOwner

kalibro_configuration_attributes.map { |kalibro_configuration_attribute|
begin
self.find(kalibro_configuration_attribute.kalibro_configuration_id)
rescue Likeno::Errors::RecordNotFound
nil
end
}.compact
end
attr_writer :attributes

def self.public
self.public_or_owned_by_user
Expand Down
14 changes: 1 addition & 13 deletions app/models/project.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
class Project < KalibroClient::Entities::Processor::Project
include KalibroRecord
include HasOwner

attr_writer :attributes

def self.public_or_owned_by_user(user = nil)
project_attributes = ProjectAttributes.where(public: true)
project_attributes += ProjectAttributes.where(user_id: user.id, public: false) if user

project_attributes.map do |attribute|
begin
self.find(attribute.project_id)
rescue Likeno::Errors::RecordNotFound
nil
end
end.compact
end

def self.latest(count = 1)
all.sort { |one, another| another.id <=> one.id }.select { |project|
attributes = project.attributes
Expand Down
15 changes: 2 additions & 13 deletions app/models/reading_group.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
class ReadingGroup < KalibroClient::Entities::Configurations::ReadingGroup
include KalibroRecord
attr_writer :attributes

def self.public_or_owned_by_user(user=nil)
reading_group_attributes = ReadingGroupAttributes.where(public: true)
reading_group_attributes += ReadingGroupAttributes.where(user_id: user.id, public: false) if user
include HasOwner

reading_group_attributes.map { |reading_group_attribute|
begin
self.find(reading_group_attribute.reading_group_id)
rescue Likeno::Errors::RecordNotFound
nil
end
}.compact
end
attr_writer :attributes

def self.public
self.public_or_owned_by_user
Expand Down
14 changes: 1 addition & 13 deletions app/models/repository.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
class Repository < KalibroClient::Entities::Processor::Repository
include KalibroRecord
include HasOwner

attr_writer :attributes

def self.public_or_owned_by_user(user = nil)
repository_attributes = RepositoryAttributes.where(public: true)
repository_attributes += RepositoryAttributes.where(user_id: user.id, public: false) if user

repository_attributes.map do |attribute|
begin
self.find(attribute.repository_id)
rescue Likeno::Errors::RecordNotFound
nil
end
end.compact
end

def self.latest(count=1)
all.sort { |one, another| another.id <=> one.id }.select { |repository| repository.attributes.public }.first(count)
end
Expand Down

0 comments on commit a5f2d20

Please sign in to comment.