Skip to content

Instantly share code, notes, and snippets.

@krisleech
Created November 16, 2012 13:24
Show Gist options
  • Save krisleech/4087333 to your computer and use it in GitHub Desktop.
Save krisleech/4087333 to your computer and use it in GitHub Desktop.
domain and persistence
class Domain::Trip
include Virtus
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attribute :id
attribute :name
attribute :sections, Array[Section], :default => []
end
class Persistence::Trip < ActiveRecord::Base
def self.find_by_id(id)
record = super(id)
Domain::Trip.new(record.attributes).tap do |trip|
trip.sections = record.sections.map { |section_record| Domain::Trip::Section.new(section_record.attributes) }
end
end
end
def show
@trip = Persistence::Trip.find(params[:id)
end
- @trip.sections.each do |section|
= section.name
@krisleech
Copy link
Author

Thanks - I like the idea of having TripSummary, after all it does not need to contain any validations, or anything else that acts on state changes, its being used in a read-only context. I guess I need to get over fear of adding classes which have overlapping responsibilities, its marginally less DRY but simpler than trying cram everything in to one class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment