Code Monkey home page Code Monkey logo

guides's People

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

guides's Issues

Ruby

Finished first draft. Might add some examples in a separate file.

Don't query data in before_actions

It's probably my fault we have this in our code, so I'll just make the first step to stop us from doing it in the future.

I started to put queries in before_actions because it looked DRY to me. For example instead of writing

class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
  end

  def edit
    @user = User.find(params[:id])
  end
end

I wrote

class UsersController < ApplicationController
  before_action :find_user, only: [:show, :edit]

  def show
  end

  def edit
  end

  private

  def find_user
    @user = User.find(params[:id])
  end
end

There are many problems with this.

  • It's not really more DRY then calling the method in each action, instead of the method names you have to list the actions in the before_filter.
  • They abstract the flow of the action from the developer.
  • And they elevate the live time of the variables.

I found a good blog post, which get's into this in way more depth - before_action an anti-pattern?.

For me, the ideal would probably be to use the before_action as they were first intended as a filter for authorisation and such (hence the name before_filter) and move the queries into the actions.

class UsersController < ApplicationController
  def show
    @user = find_user
  end

  def edit
    @user = find_user
  end

  private

  def find_user
    User.find(params[:id])
  end
end

Any thoughts?

JSON API standard

We should decide on a standard for all our JSON APIs.

The two big ones would be JSON API and HAL.

As the JSON API site points out:

By following shared conventions, you can increase productivity, take advantage of generalized tooling, and focus on what matters: your application.

For pagination we already looked at HAL but I think JSON API seems a lot more Rails-ish and is designed by the creator of Ember.js, which would probably make Ember easier if we were use it in the future.

Furthermore, from the JSON API site: Why not use the HAL specification?.

Rails

Finished first draft.

Naming

Finished first draft.

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.