Skip to content

Instantly share code, notes, and snippets.

@plainprogrammer
Last active December 1, 2017 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plainprogrammer/1b747223c67c008f6356a75842cd57a3 to your computer and use it in GitHub Desktop.
Save plainprogrammer/1b747223c67c008f6356a75842cd57a3 to your computer and use it in GitHub Desktop.
Applying Clean Architecture: Repository
module Repositories
class BusinessProfiles
def find business_id
data_model = ::BusinessProfile.find_by business_id: business_id
if data_model
fields = fields_for data_model
profile = Nav::Entities::BusinessProfile.new data_model.business_id
fields.each { |name, value| profile.send(:"#{name}=", value) }
else
profile = Nav::Entities::BusinessProfile.new 0
profile.errors << "can not find Business Profile with ID #{business_id}"
end
profile
end
def update for_id:, fields: {}
ActiveRecord::Base.transaction do
profile = BusinessProfile.find_or_create_by! business_id: for_id
fields.each do |name, value|
Field.create! business_profile: profile,
name: name,
value: value,
reported_at: DateTime.now
end
end
find for_id
end
private
def fields_for data_model
# ...
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment