Coder Social home page Coder Social logo

mvc_in_action's Introduction

MVC in Action


Setup instructions:

  - Clone this repo
  - `cd <repo_name>`
  - `bundle install`
  - `rails db:{create,migrate,seed}`

Ruby version:

2.7.4

Rails version:

5.2.6

Overview

This repository is a tool to use in conjunction with the MVC in Action lesson. There are no tests in this repository but you can use localhost:3000 to see what is happening. The models, controllers and views have been set up already. We will be focusing on identifying where we are breaking MVC logic conventions and refactoring to follow MVC logic conventions.

Everything in this repository is set up following the below code snippets. Looking at each of these, identify where MVC logic is being broken and work to correct it.

Models

class Comedian < ApplicationRecord
  has_many :specials

  def self.average_age
    "#{average(:age).round(2)} Years"
  end

  def average_special_runtime
    specials.average(:runtime)
  end
end

Views

<% @comedians.each do |comic| %>
<h4><%= comic.specials.count %> Specials</h4>
<% end %>
<h1><%= @comedian.name %></h1>
<% if @comedian.specials > 2 && @longest_special > 20 %>
  <p>Average runtime of all this comedian's specials: <%= @average_special_runtime %></p>
<% end %>

Controllers

class ComediansController < ApplicationController
  def index
    @comedians = Comedian.all
    @average_age = Comedian.average(:age)
  end
end
class ComediansController < ApplicationController
  def show
    @comedian = Comedian.find(params[:id])
    @average_special_runtime = @comedian.average_special_runtime.round(2)
    @longest_special = @comedian.specials.order(runtime: :desc).first
  end
end

For additional guidance you can consider the following rules:

  • No query logic in your Controllers or Views; this should live in your Models.
  • No data formatting in your Models or Controllers; this should live in your Views.

mvc_in_action's People

Contributors

cjsim89 avatar dcoleman21 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

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.