Notes posted by joshuapinter
RSS feed![Default_avatar_30](https://www.gravatar.com/avatar/6e6e22d81a1f4f394f62301be40c2e20?default=http://apidock.com/images/default_avatar_30.png&size=30)
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.
![Default_avatar_30](https://www.gravatar.com/avatar/6e6e22d81a1f4f394f62301be40c2e20?default=http://apidock.com/images/default_avatar_30.png&size=30)
Thanks davinjay!
Your note was SUPER helpful so I wanted to leave more than just a “1 thank”.
Cheers!
![Default_avatar_30](https://www.gravatar.com/avatar/6e6e22d81a1f4f394f62301be40c2e20?default=http://apidock.com/images/default_avatar_30.png&size=30)
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.
![Default_avatar_30](https://www.gravatar.com/avatar/6e6e22d81a1f4f394f62301be40c2e20?default=http://apidock.com/images/default_avatar_30.png&size=30)
If you need to check for overlaps in Times, use the validates_overlap gem.
github.com/robinbortlik/validates_overlap
![Default_avatar_30](https://www.gravatar.com/avatar/6e6e22d81a1f4f394f62301be40c2e20?default=http://apidock.com/images/default_avatar_30.png&size=30)
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.
![Default_avatar_30](https://www.gravatar.com/avatar/6e6e22d81a1f4f394f62301be40c2e20?default=http://apidock.com/images/default_avatar_30.png&size=30)
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"
![Default_avatar_30](https://www.gravatar.com/avatar/6e6e22d81a1f4f394f62301be40c2e20?default=http://apidock.com/images/default_avatar_30.png&size=30)
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"
![Default_avatar_30](https://www.gravatar.com/avatar/6e6e22d81a1f4f394f62301be40c2e20?default=http://apidock.com/images/default_avatar_30.png&size=30)
`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'.
![Default_avatar_30](https://www.gravatar.com/avatar/6e6e22d81a1f4f394f62301be40c2e20?default=http://apidock.com/images/default_avatar_30.png&size=30)
If you want to do a quick delete with no callbacks, use `delete_all`.
Person.where( thing: nil ).delete_all
![Default_avatar_30](https://www.gravatar.com/avatar/6e6e22d81a1f4f394f62301be40c2e20?default=http://apidock.com/images/default_avatar_30.png&size=30)
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"
![Default_avatar_30](https://www.gravatar.com/avatar/6e6e22d81a1f4f394f62301be40c2e20?default=http://apidock.com/images/default_avatar_30.png&size=30)
prepend_before_filter
If you need the method to be called at *the beginning* of the before_filter chain then you should use:
![Default_avatar_30](https://www.gravatar.com/avatar/6e6e22d81a1f4f394f62301be40c2e20?default=http://apidock.com/images/default_avatar_30.png&size=30)
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 )
![Default_avatar_30](https://www.gravatar.com/avatar/6e6e22d81a1f4f394f62301be40c2e20?default=http://apidock.com/images/default_avatar_30.png&size=30)
Use `update_all` for Updating the Same Column for Multiple Records.
http://apidock.com/rails/ActiveRecord/Relation/update_all
Client.where( country: 'Canada' ).update_all( country_code: 'CA' )
![Default_avatar_30](https://www.gravatar.com/avatar/6e6e22d81a1f4f394f62301be40c2e20?default=http://apidock.com/images/default_avatar_30.png&size=30)
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.
![Default_avatar_30](https://www.gravatar.com/avatar/6e6e22d81a1f4f394f62301be40c2e20?default=http://apidock.com/images/default_avatar_30.png&size=30)
Starts with a Capital Letter
(or any regular expression you’d like)
'Abracadabra'[0..0] =~ /[A-Z]/ # => true
![Default_avatar_30](https://www.gravatar.com/avatar/6e6e22d81a1f4f394f62301be40c2e20?default=http://apidock.com/images/default_avatar_30.png&size=30)
![Default_avatar_30](https://www.gravatar.com/avatar/6e6e22d81a1f4f394f62301be40c2e20?default=http://apidock.com/images/default_avatar_30.png&size=30)
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'
![Default_avatar_30](https://www.gravatar.com/avatar/6e6e22d81a1f4f394f62301be40c2e20?default=http://apidock.com/images/default_avatar_30.png&size=30)
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'
![Default_avatar_30](https://www.gravatar.com/avatar/6e6e22d81a1f4f394f62301be40c2e20?default=http://apidock.com/images/default_avatar_30.png&size=30)
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
![Default_avatar_30](https://www.gravatar.com/avatar/6e6e22d81a1f4f394f62301be40c2e20?default=http://apidock.com/images/default_avatar_30.png&size=30)
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
![Default_avatar_30](https://www.gravatar.com/avatar/6e6e22d81a1f4f394f62301be40c2e20?default=http://apidock.com/images/default_avatar_30.png&size=30)
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
![Default_avatar_30](https://www.gravatar.com/avatar/6e6e22d81a1f4f394f62301be40c2e20?default=http://apidock.com/images/default_avatar_30.png&size=30)
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 ]
![Default_avatar_30](https://www.gravatar.com/avatar/6e6e22d81a1f4f394f62301be40c2e20?default=http://apidock.com/images/default_avatar_30.png&size=30)
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.
![Default_avatar_30](https://www.gravatar.com/avatar/6e6e22d81a1f4f394f62301be40c2e20?default=http://apidock.com/images/default_avatar_30.png&size=30)
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']
![Default_avatar_30](https://www.gravatar.com/avatar/6e6e22d81a1f4f394f62301be40c2e20?default=http://apidock.com/images/default_avatar_30.png&size=30)
(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.
![Default_avatar_30](https://www.gravatar.com/avatar/6e6e22d81a1f4f394f62301be40c2e20?default=http://apidock.com/images/default_avatar_30.png&size=30)
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.