Categories
JavaScript

Javascript: automatic type convertion

if(something){

// Do this

}

Do this will happen, if “something” is equal to:

undefined

null

0 (as in the number)

“” (empty string)

NaN (not a number object)

Be careful! Any kind of object, or even the string “0” will evaluate to true!

The reason stuff like this works:

var myvar = me || you;

is because if “me” evaluates to true, that is what’s returned. Otherwise “you” will. That is assuming we don’t know if “me” or “you” are undefined at the moment.

 

Categories
Ruby

Ruby on Rails: write to the file system

File.open(‘theoutput.log’, ‘w’) { |file| file.write(“this will end up in your file) }

Categories
CSS3

CSS3: box-sizing property

A bit of background history.

1) IE5 and below, when you specify padding / border  in a div box, the content inside the box was shrinking to accommodate the extra space of those values. If you had a 200px box, with a 10px border and 10px margin, the content shrank to 180px.

2) IE6 and above, padding / border was pushing the box in the example above to 220px instead (the content reminds 200px, the rest is taken from outside the box, and push all the other elements around to make space for it.

3) Sometimes that doesn’t work well with fluid layout, and those extra pixels push stuff out of place, it would be ideal to tell DOM boxes to behave like IE5, pushing for extra pixels inwards instead of outwards their box limit.

4) Meet box-sizing property, since IE8 onwards you can do that:

box-sizing: content-box|border-box|inherit:

content-box is the default behavior (it pushes stuff out to make space for padding and borders)

border-box: in pushes stuff inside the DOM element, reducing its internal size, to accommodate padding and border. You can use it to make sure your fluid elements won’t push stuff out of place

Note: the default behavior for DOM elements is content-box, except when you don’t explicitly specify the width of an element, or if the element is set to be 100% width, tables and table elements are also using this “substractive behavior” by default.

 

Categories
Heroku

Heroku: sending mail

It is so easy now that mandrill by MailChimp is part of the deal.

1) Add the Mandrill Add On to your site.

2) Include the ‘mail’ gem in your gemset.

3) Example of how to send stuff:

 

require ‘mail’

Mail.defaults do
delivery_method :smtp, {
:port => 587,
:address => “smtp.mandrillapp.com”,
:user_name => “your mandrill username here”,
:password => “your mandrill API key”
}
end

mail = Mail.deliver do
to ‘somebody@gmail.com’
from ‘JohnYo DoeMero <johnmero@somewhere.com>’
subject ‘Test email via Mandrill!’

text_part do
body ‘This is the email body message’
end

html_part do
content_type ‘text/html; charset=UTF-8’
body ‘<em>And, the HTML counterpart <strong>is working!</strong></em>’
end
end

Categories
Rails

Ruby on Rails: Making Rake do tasks

First, you generate the task via the command line:

$ rails g task your_task_name_here

Then, open the newly generated task file (lib/tasks/your_task_name_here.rake

And fill out the guts (an example here):

namespace :crawl_quotes do
desc “Crawls for stuff”
task :get_stuff => :environment do
require ‘mechanize’
agent = Mechanize.new

agent.get(“http://www.somewebpagehere.com”)

agent.page.search(“.somecssclass”).search(‘a’).each do |index, item|
# Do something here
end

end

Once that is setup, all you have to do is run the task via the command line (or any other way you have for running scripts):

$rake crawl_quotes:get_stuff      (that is the name of the task namespace, the :, and the specific task you are running

Categories
HTML5

HTML5: Form fields

See them in action here

The date input field:

<input type="date"  min="2012-03-12" step="1"
                         max="2012-05-12" name="departingDate" />

 

The range input field:

<input type="range" name="shoeSize" min="0" max="15" step=".5" 
value="3" />

 novalidate will skip the browser built in html5 validation 

Categories
Rails

Ruby On Rails: devise basics

Installation

In your gem file:

gem “devise”

$ bundle install
$ rails g devise:install

After that, your config files will be at:

config/initializers/devise.rb

Time to configure the user’s model:

$ rails g devise user
$ rake db:migrate

Note that at this point, you have a set of automatic views that are internal to devise. It is advisable that you run the following command, so you can customize those yourself in your views:

rails generate devise:views

 

Set pages so only login users can see them

In your controller, add the following line:

before_filter :authenticate_user!, :only => [:index, :new, :destroy]

Helpers

current_user contains information about the current user. for example current_user.email

user_signed_in? boolean to test if a user is signed in or not, use it as:

<% if user_signed_in? %>
  <%= link_to 'Sign Out', destroy_user_session_path, method: :delete %>
<% end %>

user_session is the session attached to the user, you can store stuff in it like:
user_session[:somevarname] = “somevarvalue”

Options
In the user’s model, confirmable takes care of sending a confirmation email to newly created accounts

:confirmable

You also need to add a migration along with the confirmable option:

class AddConfirmableToUsers < ActiveRecord::Migration
  def up
    add_column :users, :unconfirmed_email, :string
    add_column :users, :confirmation_token, :string
    add_column :users, :confirmed_at, :string
    add_column :users, :confirmation_sent_at, :datetime

    add_index :users, :confirmation_token, :unique => true

    User.update_all(:confirmed_at => Time.now) #your current data will be treated as if they have confirmed their account
  end

  def down
    remove_column :users, :unconfirmed_email, :confirmation_token, :confirmed_at, :confirmation_sent_at
  end
end