• Stars
    star
    289
  • Rank 140,260 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 10 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Code samples for RailsConf 2014 on Fat Code Refactoring

Refactoring Fat Models, Controllers, and Views Example

RailsOnMaui Links to slides, talk, etc.

This example application covers four Rails Refactoring techniques: Concerns, Decorators, Presenters, and moving code to models. It builds on the Ruby on Rails Tutorial: sample application by Michael Hartl: Ruby on Rails Tutorial: Learn Web Development with Rails. However, it is changed in that minors cannot post profanity. If you login as "[email protected]", password "foobar", you get the minor profanity checking behavior. There is no UI for setting a user to be a minor, FYI.

The code is carefully crafted so that the initial code (at branch railsconf-start is relatively "clean". The lessons can thus focus only on the refactoring techniques, which are presented several pull requests. Much of the lesson information is contained in the pull request description.

Please inspect the individual commits, as the refactorings are broken up into smaller staps. The final result, the tip of the branch, contains the full refactoring. Please feel free to comment on the pull requests and/or submit issues. You may also contact me directly at [email protected].

The branches labeled rc- are the final versions developed for my RailsConf 2014 presentation, titled "Concerns, Decorators, Presenters, Service Objects, Helpers, Help Me Decide!".

Topic Branch Pull Request
Concerns rc-concerns #9
Decorators rc-decorators #10
Presenters rc-presenters #11
Models rather than Service Objects rc-business-logic-in-model #15
Split Controllers rc-split-controller #13
Controller Concerns rc-controller-concerns #14

Note, I originally planned to cover "Service Objects". However, my example of a refactoring the "Kid Safe Microblogger" actually demonstrated how a Service Object pattern is not needed. Instead, the lesson is to convert to using core Rails techniques of moving logic to business models. You can find the prior two refactoring attempts here:

Setup

The command guard both runs the tests and the application. To re-run all tests, in the guard console window, type a <return>.

This will create a branch called refactoring-tutorial where you can follow the examples.

$ cd /tmp
$ git clone https://github.com/justin808/fat-code-refactoring-techniques.git
$ git checkout railsconf-start
$ git checkout -b refactoring-tutorial
$ cd fat-code-refactoring-techniques
$ cp config/database.yml.example config/database.yml
$ bundle install
$ bundle exec rake db:migrate
$ bundle exec rake db:test:prepare
$ guard

Then, if you want to see the completed application:

$ git checkout railsconf-finish
$ bundle install
$ guard

I would suggest creating your refactoring-tutorial branch and then manually applying the commits in the pull requests, creating your own commits along the way.

At each commit, the tests should continue to pass.

You can simulate the flow of what I'll be doing in the presentation with the git scripts in bin/git-railsconf.zsh.

$ cd <top level of git repo>
$ . bin/git-railsconf.zsh
$ git checkout railsconf-start # you're at the beginning of the refactorings

Then you can run this to simulate the first edit:

$ railsconf-start

Take a look at the files that are changed. See the tests pass. Experiment. Then run:

$ railsconf-advance-history

That will move the history forward one step, and the changing files will be in the git index.

WARNING: These scripts mercilessly do git reset --hard. So beware!

More Repositories

1

react_on_rails

Integration of React + Webpack + Rails + rails/webpacker including server-side rendering of React, enabling a better developer experience and faster client performance.
Ruby
5,066
star
2

react-webpack-rails-tutorial

Example of integration of Rails, react, redux, using the react_on_rails gem, webpack, enabling the es7 and jsx transpilers, and node integration. And React Native! Live Demo:
Ruby
1,714
star
3

bootstrap-loader

Load Bootstrap styles and scripts in your Webpack bundle
JavaScript
1,024
star
4

sass-resources-loader

SASS resources (e.g. variables, mixins etc.) loader for Webpack. Also works with less, post-css, etc.
JavaScript
979
star
5

cypress-on-rails

Use cypress.io or playwright.dev with your rails application
HTML
406
star
6

shakapacker

Use Webpack to manage app-like JavaScript modules in Rails
Ruby
390
star
7

re-formality

Form validation tool for reason-react
Reason
245
star
8

bootstrap-sass-loader

Webpack Loader for the Sass version Twitter Bootstrap
JavaScript
118
star
9

rescript-dnd

Drag-n-drop for @rescript/react
ReScript
115
star
10

rescript-classnames

Reimplementation of classnames in ReScript
ReScript
110
star
11

rescript-logger

Logging implementation for ReScript
ReScript
107
star
12

react_on_rails_demo_ssr_hmr

react_on_rails tutorial demonstrating SSR, HMR fast refresh, and Typescript based on the rails/webpacker webpack setup
Ruby
86
star
13

font-awesome-loader

Webpack Loader for Font Awesome Using Sass
JavaScript
47
star
14

webpacker_lite

Slimmed down version of Webpacker with only the asset helpers optimized for, but not requiring, React on Rails
Ruby
46
star
15

mirror-creator

One more way to create an object with values equal to its key names
JavaScript
43
star
16

rescript-debounce

Debounce for ReScript
ReScript
42
star
17

steward

Task runner and process manager for Rust
Rust
41
star
18

control-plane-flow

The power of Kubernetes with the ease of Heroku! Our playbook for migrating from Heroku to Control Plane, controlplane.com, and CPL CLI source
Ruby
37
star
19

reactrails-react-native-client

This repository is for a react-native client to the https://www.reactrails.com/, source at https://github.com/shakacode/react-webpack-rails-tutorial/.
JavaScript
30
star
20

redux-tree

An alternative way to compose Redux reducers
JavaScript
23
star
21

rust-rescript-demo

Rust
22
star
22

messagebus

Message bus - enables async communication between different parts of application using messages
Rust
22
star
23

redux-interactions

Composing UI as a set of interactions
21
star
24

ssr-rs

Server-Side Rendering for Rust web servers using Node.js
Rust
20
star
25

bootstrap-sass-loader-example

Example of using the bootstrap-sass-loder to load the Sass version of Twitter Bootstrap using Webpack
JavaScript
16
star
26

style-guide-javascript

ShakaCode's JavaScript Style Guide (Also see our Ruby one: https://github.com/shakacode/style-guide-ruby)
JavaScript
15
star
27

jstreamer

Ruby
12
star
28

justerror

Extension to `thiserror` that helps reduce the amount of handwriting
Rust
12
star
29

react-validation-layer

An opinionated form validation tool for React apps
JavaScript
11
star
30

rescript-react-on-rails

BuckleScript bindings to react-on-rails
ReScript
8
star
31

todos-react-native-redux

Todos app with React Native and Redux
JavaScript
7
star
32

rescript-react-on-rails-example

Example of https://rescript-lang.org/ with React on Rails
Ruby
7
star
33

conform

Macro to transform struct string fields in place
Rust
7
star
34

rescript-first-class-modules-usage

CSS
6
star
35

rescript-throttle

Throttle for ReScript
ReScript
5
star
36

webpacker-examples

Ruby
5
star
37

package_json

Ruby
4
star
38

react_on_rails-with-webpacker

Prototype of webpacker integration
Ruby
4
star
39

use-ssr-computation.macro

A babel macro for reducing initial bundle-size of Apps with React SSR. Uses conditional compilation to make computations server-side and pass the results to the client.
TypeScript
4
star
40

react_on_rails-generator-results

Results of running different react_on_rails generators. See https://github.com/shakacode/react_on_rails
4
star
41

vosk-rs

Rust
3
star
42

react-starter-kit-with-bootstrap-loader

JavaScript
3
star
43

react_on_rails-generator-results-pre-0

Ruby
2
star
44

js-promises-testing

JavaScript
2
star
45

react-on-rails-demo

Example of using the generator for React on Rails
Ruby
2
star
46

uber_task

Ruby
2
star
47

node-webpack-async-example

Simple example of setting up node with webpack
JavaScript
2
star
48

egghead-add-redux-component-to-react-on-rails

Source for Egghead lesson: Add Redux State Management to a React on Rails Project
Ruby
1
star
49

reactrails-in-reagent

Test of converting reactrails example to reagent
1
star
50

rails-tutorial-with-react-on-rails

Adding React on Rails to the Rails Tutorial
Ruby
1
star
51

.github

1
star
52

react_on_rails-test-new-redux-generation

Testing out a different redux generation. See https://github.com/shakacode/react_on_rails/pull/584
Ruby
1
star
53

yardi

Yet Another Rust Dependency Injector
Rust
1
star
54

old-react-on-rails-examples

Various example apps for React on Rails, outdated
JavaScript
1
star
55

v8-demo

React on Rails v8 Usage of the basic generator and a "generator function example"
Ruby
1
star
56

docker-ci

docker container that runs all test and linting against app
Ruby
1
star
57

react-rails-webpacker-resources

1
star
58

sass-rel-paths-issue

JavaScript
1
star
59

reactrails-in-om-next-example

Test of converting reactrails example to om-next
1
star
60

homebrew-brew

Ruby
1
star
61

zone-helper

Swift
1
star
62

react_on_rails-update-webpack-v2

Updating the default React on Rails 6.6.0 redux installation to use Webpack v2 plus adding eslint.
Ruby
1
star
63

shakacode-website

Public website and blog
HTML
1
star
64

react-native-image-zoom-slider

JavaScript
1
star
65

derive-from-one

Automatically generates `From` impls so you don't have to
Rust
1
star
66

rescript-on-rails-example

A sample application using Rescript and React on Rails.
Ruby
1
star