Skip to content

Instantly share code, notes, and snippets.

@matt-the-wizard
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matt-the-wizard/b2ff5756bdf7ce891d2d to your computer and use it in GitHub Desktop.
Save matt-the-wizard/b2ff5756bdf7ce891d2d to your computer and use it in GitHub Desktop.
I learned the use of different gems for my rails project.

Gems with Brew-Ville

Check out brew-ville here!

Welcome to Brew-Ville! You can learn a lot about gems and adding their functionality to your rails app by looking at the samples I provided in this gist. I experimented with some gems that I have never used before and provided code samples and pictures to show you how it works. If you are new to ruby on rails, gems are libraries that you can add into your application to provide features like encryption or seeding test data for your database. There are all kinds of gems you can research like divise for adding users to your website. All you literally have to do is go to your root directory of your rails app and type

bundle install 'gemname'

From a java background, it is like adding an external JAR to your app. Rails has a lot of configuration, so it is a good idea to extract as much functionality as you need from gems so you don't have to maintain that code in house.

Pagination

paginate

Have you ever wanted an easy way to handle listing hundreds or thousands of objects on a web page? If only you could add links between all the search pages to show objects on page 1, objects on page 2............wait, there is Will Paginate!

  • Pagination is used to handle a collection of active record objects in the view. It creates multiple linked pages to show a certain number of objects per page.
  • It will render a view file called a partial, which is the object name from active record with an underscore in front of it.
    • In app/view/users you will see a _user.html.erb, which the underscore and the model name user denotes that view file as a partial.(It is only part of a view file) user
* This file will be rendered for every user object that is paginated. 
  • This is a very handy gem for showing lots of objects like users on a website, and it is easy to modify how each one is shown by updating the partial.
  • Each user you see in the list with their profile picture is a rendered partial.
    @users = User.paginate(page: params[:page]).order(:name)

Bootstrap with SASS

Bootstrap is a gem that imports the html styling library used by the creators of twitter. This is very handy if you are like me and struggle with styling and would rather write code than mess around with buttons and widgets. SASS is syntactically awesome style sheets. SASS validatse css that is written as well as lets you nest styling for child objects inside styling for parent objects. The two of these gems together makes some very powerful styling in a short amount of time, super handy. All you have to do is map html tags to classes that are already made in the boostrap library and then modify the styling to your preference. A list of bootstrap components is found here: http://getbootstrap.com/components/ bootstrap jumbo

  • Notice how the styling is nested in the div classes in the scss file. The code also shows how you can modify existing bootstrap styled objects, so making a few simple changes can modify bootstrap to your sites theme.
  • The nice things about SASS is that by nesting the styling of objects it makes it much easier to maintain and read your styling code. In CSS, or regular cascading stylesheets, you had to individually specify every single nested parent child styling relationship. It was a major pain in the but. Now you can spend a lot more time writing the backend that is gonna run the application.
  • Reference: http://getbootstrap.com/

Faker

faker

Faker is a gem used to create test data for testing your application. It will generate random data for various attribute types that you can use to populate objects and seed your database.

  • Faker is super handy for populating your site will all kinds of stuff so you can see how it handles on performance as well as styling bugs.
  • Faker uses lorem ipsum to generate fake text that is gibberish with no meaning, basically placeholder text. It can also generate test data for phone numbers, dates, and all kinds of various objects.
  • Reference: http://www.rubydoc.info/github/stympy/faker/master/frames

Other Cool Thing That Isn't A Gem: Sorting by Column Names on a Table

sortable

I followed a tutorial by Rails Cast to learn how to sort all my objects in my beers table when I am showing all the beers on the site. I set it up to where the user can click a column title and it will sort all objects based on that title, which is really representing an attribute. If they click the title again, it will go from ascending to descending. Pretty Cool!

In the Beers Controller I decided that I would load all my beer objects based on them being sorted by the user.

def index
  @beers = Beer.order(sort_column + " " + sort_direction)
end

In the Beers Helper Module I wrote a sorting method to sort based on the title of the column chosen on the index page.

def sortable(column, title = nil)
  title ||= column.titleize
  css_class = column == sort_column ? "current #{sort_direction}" : nil
  direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
  link_to title, {:sort => column, :direction => direction}, {:class => css_class}
end

In the view file for the index route of the beer controller, I named the title columns the same as the attributes of my beer model. That way I could pass in which attribute I wanted to sort by in my sort method on the helper.

<thead>
  <tr>
    <th><%= sortable "title" %></th>
    <th><%= sortable "abv" %></th>
    <th><%= sortable "description" %></th>
    <th>User</th>
    <th colspan="3"></th>
  </tr>
</thead>

Reference: http://railscasts.com/episodes/228-sortable-table-columns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment