Ruby Tips #334 – Ruby Flow Control and Assignment

Ruby offers flow control and assignment and it can be flipped either on the right or left side of ‘if’ and ‘unless’ statements. if registered and remote_app = RemoteApplications.find(app_id) # do many things end # or ... if remote_app = RemoteApplications.find(app_id) and registered # do many things end

Ruby Tips #333 – Touch Events in Coffeescript (Part 2)

To build on yesterday’s tip let’s look at a simple example indicating the direction of our users touch. # app/assets/javascripts/ movement = element: $('body') startingPointX: 0 startingPointY: 0 movement.touching = (e) -> e = e.originalEvent touchObject = e.changedTouches[0] point = if e.touches then e.touches[0] else e movement.startingPointX = point.pageX...

Ruby Tips #332 – Touch Events in Coffeescript (Part 1)

Although not Ruby, Coffeescript is part of every Rails developer’s toolkit now, and with front-end development comes the responsibility of responsive design. Today we look at how to grab a moving finger on a mobile device. In part two we will look at how to test the direction of...

Ruby Tips #331 – Lock Down Beta Sites with Before_action

Building beta sites can become cluttered with statements, but Rails before_actions make it easy to lock down sections of web applications without flow control in views. class PagesController < ApplicationController before_action :redirect_unless_live, only: [:tickets, :shows, :press] def press; end def shows; end def tickets; end home # ready for...

Ruby Tips #330 – Lock Down Devise when Using OAuth

If you use devise for authentication management with OAuth and a third party you may need to remove the devise authentication routes and confirm your model won’t be able to create rouge users. This solution will prevent users getting to your authentication routes and prevent non-approved users from entering...

Ruby Tips #329 – Format Ruby Hash in Query Parameters

Yet another way to join to join hash parameters for url construction is to mock Ruby’s join method. parameters = {title: 'president', name: 'Barack'} { |i| i * "=" } * "&" #=> "name=Barack&title=president" 🙂

Ruby Tips #328 – Quick JS Templates in Coffeescript

The jQuery append call makes it easy to have minimal javascript templates in your coffeescripts. # app/assets/javascripts/ obj = {title: "freedom party", link: ""} template = """ #{obj.title} """ $('#funny_stuff').html(template) 🙂

Ruby Tips #327 – to_sql and to_a for ActiveRecord Relations

ActiveRecord relation objects have a couple of methods that make our job a bit easier. Rails to_sql and Rails to_a allow to see the query without running the action and to_a allows for a throwback to the older days when ActiveRecord returned arrays not relation objects. >> User.where(:beta_user =>...

Ruby Tips #326 – Get mp4 Information from Rails Console

There are two requirements to get to retrieving information from Rails console. 1) $ brew install ffmpeg 2) In your Gemfile add `gem ‘streamio-ffmpeg’` 3) Now load up a Rails console and get some data irb(main):001:0> movie ="~/Movies/clips/evilminion_ilSJ9olk.mp4") => # movie.duration => 46.09 🙂

Ruby Tips #325 – Another Javascript Tip ‘originalEvents’

When working with mobile browsers in Rails it is important note jQuery doesn’t always give you the full Javascript API. jQuery will strip some event properties to conserve memory. In this case let’s look at the ‘touchmove’ event. To get all the properties, in pure jQuery, of a ‘touchmove’...