9.3 Iteration D3: Adding a button 9.1 Iteration D1: Finding a Cart
Create line item which connects products to carts'
Create the model object.
rails generate scaffold LineItem product:references cart:belongs_to
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
invoke active_record
create db/migrate/20140204194146_create_line_items.rb
create app/models/line_item.rb
invoke test_unit
create test/unit/line_item_test.rb
create test/fixtures/line_items.yml
route resources :line_items
invoke scaffold_controller
create app/controllers/line_items_controller.rb
invoke erb
create app/views/line_items
create app/views/line_items/index.html.erb
create app/views/line_items/edit.html.erb
create app/views/line_items/show.html.erb
create app/views/line_items/new.html.erb
create app/views/line_items/_form.html.erb
invoke test_unit
create test/functional/line_items_controller_test.rb
invoke helper
create app/helpers/line_items_helper.rb
invoke test_unit
create test/unit/helpers/line_items_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/line_items.js.coffee
invoke scss
create app/assets/stylesheets/line_items.css.scss
invoke scss
identical app/assets/stylesheets/scaffolds.css.scss
rake db:migrate
mv 20140204194146_create_line_items.rb 20140204000003_create_line_items.rb
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
== CreateLineItems: migrating ================================================
-- create_table(:line_items)
-> 0.0009s
-- add_index(:line_items, :product_id)
-> 0.0004s
-- add_index(:line_items, :cart_id)
-> 0.0005s
== CreateLineItems: migrated (0.0019s) =======================================
Cart has many line items.
edit app/models/cart.rb
class Cart < ActiveRecord::Base
has_many :line_items, :dependent => :destroy
end
Product has many line items.
edit app/models/product.rb
class Product < ActiveRecord::Base
has_many :line_items
before_destroy :ensure_not_referenced_by_any_line_item
#...
private
# ensure that there are no line items referencing this product
def ensure_not_referenced_by_any_line_item
if line_items.empty?
return true
else
errors.add(:base, 'Line Items present')
return false
end
end
end
Line item belongs to both Cart and Product (But slightly more to the Cart). Also provide convenient access to the total price of the line item
edit app/models/line_item.rb
class LineItem < ActiveRecord::Base
belongs_to :product
belongs_to :cart
end
rake test:functionals
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
Loaded suite /home/rubys/.rvm/gems/ruby-1.8.7-p374/gems/rake-10.1.1/lib/rake/rake_test_loader
Started
......................
Finished in 0.421707 seconds.
22 tests, 35 assertions, 0 failures, 0 errors
9.3 Iteration D3: Adding a button 9.1 Iteration D1: Finding a Cart