Giter Site home page Giter Site logo

using-active-model-serializer-lab-v-000's Introduction

Using ActiveModel::Serializer Lab

Objectives

  1. Use AMS to render JSON.
  2. Use AMS to render JSON associations.

Introduction

We're going to continue updating our products/orders system to use ActiveModel::Serializer for JSON serialization. The solution to the previous lab is provided.

Don't forget to run rake db:seed for some starter data and lots of Latin practice!

Instructions

  1. Create an ActiveModel::Serializer for Product and update the products_controller to use the new serializer in place of the existing to_json code.
  2. Update the product show page to handle the new JSON.
  3. On the products index page, update the More Info button so that it uses the /products/id.json route to get both description and inventory.
  • Note: The serializer will return a different value for inventory than the previous API, so you'll have to handle that.
  1. Update the ProductSerializer to include the orders for the product.
  2. Update the More Info button on the products index page to show a list of orders with id and created_at in addition to the description and inventory.
  3. Update the OrderSerializer to include the product names of all products on that order.
  4. Get rid of the unused /products/id/description and /products/id/inventory routes.
  5. Make sure tests pass!

using-active-model-serializer-lab-v-000's People

Contributors

aviflombaum avatar cernanb avatar jilustrisimo avatar maxwellbenton avatar mendelb avatar pletcher avatar scottcreynolds avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

using-active-model-serializer-lab-v-000's Issues

possible test issue for the get JSON

test finding a created by with my code, looking it up, it is not included possible error?

please pull down my code uncomment the test and see what you find?

Tests don't match readme requirements

The tests in this lab don't bother to look for several of the things listed in the readme. What's more the solution of the "using-acitive-model-serializer-lab" doesn't bother to use active model serializers. This whole thing is sloppy and slipshod.

selenium-webdriver support broken in firefox 47

If a student is having trouble with the test and getting error messages with the selenium-webdriver gem, be aware that support was broken with firefox 47, released June 7th, 2016. Replicated the issue on my local environment, downgrading to firefox 46 fixed the issue.

Error on tests

1.1) Failure/Error: visit products_path

  ```
ArgumentError:
        The detector #<struct Cliver::Detector command_arg=nil, version_pattern=nil> failed to detect theversion of the executable at '/usr/bin/phantomjs'

Problem with Faker Number Generator

Faker gem (https://github.com/stympy/faker) has updated their Faker::Number.between method.

The error is found in the following files:
invoices_index_view_spec.rb
orders_index_view_spec.rb

This is the error:
ArgumentError:
wrong number of arguments (given 2, expected 0)

Before:
Product.create(:name => products.sample, :price => Faker::Number.between(1,1500))

Fix:
Product.create(:name => products.sample, :price => Faker::Number.between(from: 1, to: 1500))

Created at not needed in serializer

Update the More Info button on the products index page to show a list of orders with id and created_at in addition to the description and inventory.

When added to the serializer's attributes, it fails the test.

Weird Products Index Test

My solution worked in the browser (below). It extended the description for each product and created some li's for the order info for each. For some reason, the products index test only passed when I commented out my forEach block. But either way it loaded the description in the browser, so I am confused why if the test was clicking the "More Info" button as it says, it wouldn't find the full description.

screen shot 2018-08-24 at 4 38 18 pm

Failure/Error: expect(page).to have_content product.description expected to find text "This is a test description with more text than should be there." in "Flatiron Widgets Store Products 2783: Test Product This is a test description ... More Info"

I tried creating a new product of my own and clicking "More Info" as well, which worked. In any case I'm curious why this would be the case-- I saw the solution for this lab, but felt this to be a little cleaner.

<% @products.each do |product| %>
  <h3><%=  product.id %>: <%= product.name %></h3>
  <div id="product-<%= product.id %>"><%= truncate(product.description) %></div>
  <ul id="order-list-<%= product.id %>"></ul>
  <button class="js-more" data-id="<%= product.id %>">More Info</button>
<% end %>

<script type="text/javascript" charset="utf-8">
$(function() {
  $(".js-more").on("click", function() {
    var id = $(this).data("id");
      $.get("/products/" + id + ".json", function(data) {

        var inventoryText = "<b>Available</b>"
        if(data["inventory"] === 0){
          var inventoryText = "<b>Sold Out</b>"
          inventoryText = "<strong>Sold Out</strong>";
        }
        var descriptionText = "<p>" + data["description"] + "</p><p>" + inventoryText + "</p>";
      $("#product-" + id).html(descriptionText);
      
       data["orders"].forEach(x => { 
          var created = x["created_at"]
          var order_id = x["id"]
          $("#order-list-" + id).append(`<li>${created}</li><li>${order_id}</li>`)
          })
    });
  });
});
</script>

active model serializer gem issues

The labs test specs and solution seem to be built under the assumption that ActiveModelSerializer 0.9.x is being used, but the master branch doesn't include the gem in the gemfile. As a result, when I added the gem to my gemfile it installed 0.10.x, which results in a different serialization format. Notably, the product information is no longer nested inside data and the has_many products relationship doesn't show up for orders when nested within products.

Using 0.9.4:
screen shot 2016-06-01 at 4 26 15 pm

Using 0.10.0:
screen shot 2016-06-01 at 4 28 26 pm

The work around that I used to maintain the has_many products relationship for orders while using 0.10.0 is to specify :products as an attribute in the Order serializer, but I'm not sure that's ideal as it reveals all of the attributes specified in the Product serializer save for the has_many orders relationship:

screen shot 2016-06-01 at 4 33 43 pm

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.