Flowdock

Notes posted by joshuapinter

RSS feed
May 28, 2019
0 thanks

to_sentence_exclusive

By default, to_sentence combines elements inclusively by using ", and ".

If you want to be exclusive and combine the elements using ", or ", you can either pass in lengthy options to to_sentence or use this handy extension I made to add to_sentence_exclusive to the Array class.

class Array

  # Adds a simple method that overloads `to_sentence` except it uses "or" instead of "and", which is the default.
  # The reason for this is because `to_sentence` is extremely flexible but that results in a lot of code to get
  # the simple result of using "or" instead of "and". This makes it simple.
  #
  def to_sentence_exclusive
    self.to_sentence( two_words_connector: " or ", last_word_connector: ", or " )
  end

end

Just drop this in config/initializers/to_sentence_exclusive.rb, restart your server or console and Bob’s your uncle.

April 19, 2019
0 thanks

Thanks davinjay!

Your note was SUPER helpful so I wanted to leave more than just a “1 thank”.

Cheers!

April 3, 2019
0 thanks

OUTDATED!!!

Man, this stuff is so outdated. Be very careful using anything from here. A lot has changed since Ruby 1.9.

You’ll want to look at the updated docs, like here for Ruby 2.5.1:

ruby-doc.org/core-2.5.1/Time.html#method-i-strftime

They really should just take this site down if they’re not going to keep it updated. Probably does more harm than good now.

May 3, 2018
0 thanks
March 22, 2018
0 thanks

Each attribute has a `reset_<attribute>!` method on it as well.

So if the attribute is name you can call reset_name! on the object to reset the dirty changes.

January 29, 2018 - (<= v3.2.13)
0 thanks

Correction: Getting just the ordinal on Rails 3.

The ordinal method isn’t publicly available in Rails 3 so you can do something like this:

ordinalize(1).last(2) #=> "st"

ordinalize(20).last(2) #=> "th"
December 9, 2017 - (<= v3.2.13)
0 thanks

Getting just the ordinal on Rails 3.

The ordinal method isn’t publicly available in Rails 3 so you can do something like this:

ordinal(1).last(2) #=> "st"

ordinal(20).last(2) #=> "th"
April 12, 2016
0 thanks

`value` or `text` method with parameter.

If your value or text method requires a parameter, like to_s(:select) then you need to create a separate method that references that in your model, like:

def to_s_select
  to_s(:select)
end

You can’t pass it in as a string like 'to_s(:select)'.

You have to use :to_s_select or 'to_s_select'.

April 29, 2015
0 thanks

Prevent new line character.

To prevent the “\n” character added at the end of each file pass in the “row_sep: nil” option:

[ "some", "array" ].to_csv                 # => "some, array\n"  

[ "some", "array" ].to_csv( row_sep: nil ) # => "some, array"
February 8, 2015
0 thanks

prepend_before_filter

If you need the method to be called at *the beginning* of the before_filter chain then you should use:

prepend_before_filter

May 1, 2014
2 thanks

Passing an array of keys to exclude.

Use the “*” before passing the array in. For example:

PARAMS_TO_SCRUB = [ :created_at, :updated, :id, :format ]

params.except!( *PARAMS_TO_SCRUB )
August 7, 2013
0 thanks

Not Supported on Memcached.

Just an FYI, this is not supported on Memcached, so I would avoid it if you’re using FileStore and are planning on moving over to Memcached eventually.

May 3, 2012
0 thanks

Starts with a Capital Letter

(or any regular expression you’d like)

'Abracadabra'[0..0] =~ /[A-Z]/       # => true
March 18, 2012
0 thanks

Use Join to Turn Array Items into a String.

If you’re looking to take an array like

[ 'don', 'draper' ]

And get

'don draper'

Then use join instead:

[ 'don', 'draper' ].join( ' ' ) 

#=> 'don draper'
March 18, 2012
0 thanks

Destructive to the Original String.

Just as an FYI this function is destructive to the original String object.

name = 'draper' #=> "draper"

name.insert( 0, 'don ' ) #=> 'don draper'

name #=> 'don draper'
January 30, 2012
2 thanks

Requires a Block.

Just a little heads up here because it’s not obvious.

This requires a block to be passed to it.

Example Usage

say_with_time "Reverting all service rates to nil." do
  Service.update_all( :rate, nil )
end

# Output
-- Reverting all service rates to nil.
   -> 0.3451s
   -> 2233 rows
January 27, 2012
0 thanks

Example Usage

End of Day for Any Date

DateTime.new( 2011, 01, 01 )
# => Sat, 01 Jan 2011 00:00:00 +0000

DateTime.new( 2011, 01, 01 ).end_of_day
# => Sat, 01 Jan 2011 23:59:59 +0000

With Local Timezone

DateTime.civil_from_format( :local, 2011, 01, 01 ).end_of_day
# => Sat, 01 Jan 2011 23:59:59 -0700
January 19, 2012
1 thank

Pluralize with Text.

Example

1 person or 3 people

Use a View Helper

pluralize( 1, 'person' )
# => 1 person

pluralize( 2, 'person' )
# => 2 people

# In practice.
pluralize( Person.count, 'person' )

See

http://apidock.com/rails/ActionView/Helpers/TextHelper/pluralize

January 4, 2012
0 thanks

Total Unique Elements from Two Arrays

Simple but thought it was worth mentioning:

( [ 1, 2, 3 ] + [ 3, 4, 5 ] ).uniq    #=> [ 1, 2, 3, 4, 5 ]
September 25, 2011
1 thank

Parsing the Date Params into a New Date Object.

Useful for when you need to create a Date object from these manually, such as for reporting.


If the date select input is start_date and it belongs to the report form object:

@start_date = Date.civil(params[:report]["start_date(1i)"].to_i,
                         params[:report]["start_date(2i)"].to_i,
                         params[:report]["start_date(3i)"].to_i)

@start_date.class
# => Date

@start_date
# => Sun, 25 Sep 2011  # For example.

Use a similar method for DateTime situations.

September 14, 2011
0 thanks

Find a Selected Option in a Drop Down.

You can find the selected option (or options for multiple select inputs) with the following:

assert_select('select#membership_year option[selected]').first['value']
September 5, 2011
1 thank

(Another) Pluralize Without Showing the Count

Thought it would be best to take the source code from pluralize and just remove the count from the output.

Create this helper method in application_helper.rb

# Pluralize without showing the count.
def simple_pluralize count, singular, plural=nil
  ((count == 1 || count =~ /^1(\.0+)?$/) ? singular : (plural || singular.pluralize))
end

This allows you to pass in in the plural word to use as well.

May 26, 2011
0 thanks

Don't Use to_formatted_s(:db) on an Array of IDs

I thought using to_formatted_s(:db) on an array of ids would separate them with commas in a nice way. Wrong. It does, but it also changes the numbers.

Wrong

[60, 271, 280, 283].to_formatted_s(:db)
# => "121,543,561,567"    # Completely different numbers!

Instead, use the join method:

Right

[60, 271, 280, 283].join(",")
# => "60,271,280,283"      # Much better

I think this has to do with (:db) being used for formatting dates but I’m not sure.