Created
March 10, 2014 23:55
-
-
Save slowjud/9476959 to your computer and use it in GitHub Desktop.
Good Bike models for my mongoid talk at RORO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Bike | |
include Mongoid::Document | |
field :brand_name | |
validates :brand_name, presence: true | |
STYLES = %w(CARBON_MONSTROSITY DUALIE FIXIE TOURER) | |
field :style | |
validates :style, inclusion: STYLES | |
field :colour | |
field :price, type: BigDecimal | |
embeds_many :parts, cascade_callbacks: true | |
# indexes | |
index brand_name: 1 | |
index style: 1 | |
index colour: 1 | |
index price: 1 | |
index brand_name: 1, style: 1 | |
index 'parts.brand_name' => 1 | |
# scopes | |
scope :sram_cranks, ->(){ | |
where( 'parts.name' => 'CRANK', | |
'parts.brand_name' => 'SRAM') | |
} | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Part | |
include Mongoid::Document | |
embedded_in :bike | |
field :name | |
validates :name, presence: true | |
field :brand_name | |
field :colour | |
validates :colour, presence: true | |
before_save :upcase_colour | |
def upcase_colour | |
self.colour = colour.upcase | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment