Rails Agile Store Basic Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Mobile Viewport

A

meta name=”viewport”, content=”width=device-width, initial-scale=1.0” >

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Product validation

A

validates :title, :description, :image_url, presence: true
validates :price, numericality: {greater_than_or_equal_to: 1}
validates :title, uniqueness: true
has_attached_file :image, :styles=> {:medium => “300x300>”, :thumb=> “100x100>”}
validates_attachment_content_type :image, :content_type =>[ “image/png” , “image/jpg”, “image/gif” ]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

D2: Connecting Products to Carts

Cart Scaffold

A

$ rails generate scaffold cart

$ rails generate scaffold line_item product:references cart:references

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

D3: model Cart.rb

model Cart.rb

A

has_many :line_items, dependent: :destroy

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

D5: model product.rb

ensure_not_referenced_by_any_line_item

A

has_many :line_items
before_destroy :ensure_not_referenced_by_any_line_item
private
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

D6: CurrentCart

application_controller.rb

A
module CurrentCart
      extend ActiveSupport::Concern
      private
        def set_cart
          @cart = Cart.find(session[:cart_id])
        rescue ActiveRecord::RecordNotFound
          @cart = Cart.create
          session[:cart_id] = @cart.id
        end
    end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

D7: Adding a Button

in app/view/store/index.html.erb

A

%= button_to ‘Add to Cart’, line_items_path(:product_id => product) %>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

D8: adding the method in Line_items_controller.rb

depot_f/app/controllers/line_items_controller.rb

A

include CurrentCart
before_action :set_cart, only: [:create]

 def create
    product = Product.find(params[:product_id])
    @line_item = @cart.line_items.build(:product => product)
respond_to do |format|
  if @line_item.save
    format.html { redirect_to(@line_item.cart, :notice => 'Line item was successfully created.') }
    format.js { @current_item = @line_item }
    format.json { render :show, status: :created, location: @line_item }
  else
    format.html { render :action => "new" }
    format.json { render json: @line_item.errors, status: :unprocessable_entity }
  end
end   end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

D9: Putting the view for Adding

in app/view/carts/show.html.erb

A

% @cart.line_items.each do |item| %>

<li>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Task E: A Smarter Cart

A

$ rails generate migration add_quantity_to_line_item quantity:integer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Task E2: Modify the migration to add default value

A
#class AddQuantityToLineItem  1
  #end
#end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Task E3: Add to cart f(x)

in app/model/cart.rb

A

class Cart :destroy

    def add_product(product_id)
    current_item = line_items.find_by(product_id: product_id)
    if current_item
      current_item.quantity += 1
    else
      current_item = line_items.build(product_id: product_id)
    end
    current_item
  end
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Task E5: update the show.html in Cart

in app/view/carts/show.html.erb

A

<p></p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Task E6: Create the cart render partial

in app/views/carts/_cart.html.erb

A

%= render(cart.line_items) %>

    Total

%= button_to (‘Empty cart’), @cart, method: :delete, data: { confirm: ‘Are you sure?’ } %>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Task E7: Create new migration

A

$ rails generate migration combine_items_in_cart

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Task E8: Method for adding items or subtracting in the custom migration (Combine UP)

A
class CombineItemsInCart  1
          # remove individual items
          cart.line_items.where(product_id: product_id).delete_all
          # replace with a single item
          item = cart.line_items.build(product_id: product_id)
          item.quantity = quantity
          item.save!
        end
      end
    end
  end

def self.down
# split items with quantity>1 into multiple items
LineItem.where(“quantity>1”).each do |line_item|
# add individual items
line_item.quantity.times do
LineItem.create cart_id: line_item.cart_id, product_id: line_item.product_id, quantity: 1
end
# remove original item
line_item.destroy
end
end

end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Task E8: CREATE, UPDATE , DESTROY Carts_controller.rb

A
def create
    @cart = Cart.new(cart_params)
respond_to do |format|
  if @cart.save
    format.html { redirect_to @cart, notice: 'Cart was successfully created.' }
    format.json { render :show, status: :created, location: @cart }
  else
    format.html { render :new }
    format.json { render json: @cart.errors, status: :unprocessable_entity }
  end
end   end

def update
respond_to do |format|
if @cart.update(cart_params)
format.html { redirect_to @cart, notice: ‘Cart was successfully updated.’ }
format.json { render :show, status: :ok, location: @cart }
else
format.html { render :edit }
format.json { render json: @cart.errors, status: :unprocessable_entity }
end
end
end

  def destroy
    @cart.destroy if @cart.id == session[:cart_id]
    session[:cart_id] = nil
    respond_to do |format|
      format.html { redirect_to store_url }
      format.json { head :no_content }
    end
  end
18
Q

Task F: Add a Dash of Ajax
Task F1.Render the views for Ajax action to add the partials
app/views/application.html.erb

A

div class=”col-xs-2”>
<div>
</div>

19
Q

Task F2. Create the Cart Partial VIew

in app/views/carts/_cart.html.erb

A
%= render(cart.line_items) %>
    tr class="total_line">
     td colspan="2">Total
      td class="total_cell">
    /tr>
/table>
%= button_to ('Empty cart'), @cart, method: :delete, data: { confirm: 'Are you sure?' } %>
20
Q

Task F3. Create the Cart Show VIew

app/views/carts/show.html.erb

A

% if notice %>
p id=”notice”>
% end %>
%= render @cart %>

21
Q

Task F4. Update the Store Controller

app/controllers/store/stores_controller.rb

A

class StoreController

22
Q

Task F5. Update the Add to cart in the Store Index page by adding Ajax
app/views/store/index.html.erb

A

%= button_to ‘Add to Cart’, line_items_path(:product_id => product) , :remote => true %>

23
Q

Task F6. Create a Rails JavaScript builder with

app/views/line_items/create.js.erb

A

$(‘#cart’).html(“”)

24
Q

Task F7. Make it visible

app/controllers/line_items_controller.rb

A
def create
    @cart = current_cart
    product = Product.find(params[:product_id])
    @line_item = @cart.add_product(product.id)
    respond_to do |format|
      if @line_item.save
        format.html { redirect_to store_url }
        format.js   { @current_item = @line_item }
      else
        format.html { render :new }
      end
    end
  end
25
Q

Task F8. Make it Shine

app/views/line_items/create.js.erb

A

$(‘#cart2’).html(“”);

$(‘#current_item’).css({‘background-color’:’#88ff88’}).animate({‘background-color’:’#114411’}, 1000);

26
Q

Task F9. Make it visible/ invisible
F9.1
app/views/carts/_cart.html.erb

A
#first line
 #
..
#
#last line
27
Q

F10 Use the helper to…HELP

app/helpers/application_helper.rb

A

module ApplicationHelper
def hidden_div_if(condition, attributes = {}, &block)
if condition
attributes[“style”] = “display: none”
end
content_tag(“div”, attributes, &block)
end
end

28
Q

Task F11 Update the destroy method

app/controllers/line_items_controller.rb

A
def destroy
    @line_item.destroy
    respond_to do |format|
      format.html { redirect_to line_items_url, notice: 'Line item was successfully destroyed.' }
      format.json { head :no_content }
    end
  end
29
Q

Part G: Checkout

G1 creating the scaffold for Order and Migrations for LineItems

A

$ rails generate scaffold order name:string address:text email:string pay_type:string
$ rails generate migration add_order_id_to_line_item order_id:integer
$ rake db:migrate

30
Q

G2 Adding the checkout button

app/views/carts/_cart.html.erb

A
# :get %>
#<div class="depot_form">
  #   
    #      
      #    </div>
31
Q

G3 Update the form

app/views/orders/_form.html.erb

A

<div>
<br></br>

</div>

<div>
<br></br>

</div>

<div>
<br></br>

</div>

<div>
<br></br>

</div>

<div>

</div>

32
Q

G4 Update the order LineItem

app/model/line_item.rb

A

class LineItem

33
Q

G5 Update the order Model

app/model/order.rb

A

class Order

34
Q

G6 Update the orders controller

app/controllers/orders/orders_controlller.rb

A
def new
    @cart = current_cart
    if @cart.line_items.empty?
      redirect_to store_url, notice: "Your cart is empty"
      return
    end
    @order = Order.new
  end
#...
def create
    @cart = current_cart
    @order = Order.new(order_params)
    @order.add_line_items_from_cart(@cart)
    respond_to do |format|
      if @order.save
        Cart.destroy(session[:cart_id])
        session[:cart_id] = nil
        format.html { redirect_to store_url, notice: 'Obrigado pelo envio da ordem' }
        format.json { render :show, status: :created, location: @order }
      else
        format.html { render :new }
        format.json { render json: @order.errors, status: :unprocessable_entity }
      end
    end
  end
35
Q

G7 Update the Create.js.erb

app/views/line_items/create.js.erb

A
#Add one line
$('#notice').hide();
36
Q

G8 Pagination

Gemfile

A

gem ‘will_paginate’

37
Q

G9 Pagination

app/controllers/orders/orders_controlller.rb

A
def index
    @orders = Order.paginate 
        :page => params[:page],
        :order=> 'created_at desc',
        :per_page => 10
   respond_to do |format|
       format.html # index.html.erb
       format.xml { render :xml => @orders }
   end
end
38
Q

G10 Index

app/views/orders/index.html.erb

A

<p></p>

39
Q

TaskH:SendingMail

A

Use the Code4Startup

40
Q

TaskI:Users

A

Use the Code4Startup

41
Q

Iteration X: Accepting Stripe Payments

A

se the Code4Startup

42
Q

D4: model LineItem.rb

A

class LineItem