This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
# what about this? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def merge_options(options, extra_options) | |
(options.keys + extra_options.keys).uniq.each do |key| | |
merge = options[key] && extra_options[key] | |
if key == :conditions && merge | |
options[key] = merge_conditions(extra_options[key], options[key]) | |
elsif key == :include && merge | |
options[key] = merge_includes(options[key], extra_options[key]).uniq | |
else | |
options[key] = options[key] || extra_options[key] | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# class User < ActiveRecord::Base | |
# named_scope :active, :conditions => { :active => true } | |
# named_scope :with_status, lambda {|*statuses| {:conditions => ["users.status IN (?)", statuses]}} | |
# named_scope :joshes, :conditions => ["first_name LIKE ?", "%Josh%"] | |
# | |
# combined_scope :active_pending, lambda { active }, lambda { with_status("pending") } | |
# combined_scope :active_unimportant, lambda { active }, lambda { with_status("not_activated", "cancelled", "deleted") } | |
# # since they are real scopes, you can combine them again; in this case, with active_pending (created with combined_scope) and joshes (created with named_scope) | |
# combined_scope :active_pending_joshes, lambda { active_pending }, lambda { joshes } | |
# end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> products = [] | |
=> [] | |
>> 1.upto(5) do |i| | |
?> products << OpenStruct.new(:part_number => "part_num_#{i}", :name => "Product #{i}") | |
>> end | |
=> 1 | |
>> products | |
=> [#<OpenStruct name="Product 1", part_number="part_num_1">, #<OpenStruct name="Product 2", part_number="part_num_2">, #<OpenStruct name="Product 3", part_number="part_num_3">, #<OpenStruct name="Product 4", part_number="part_num_4">, #<OpenStruct name="Product 5", part_number="part_num_5">] | |
>> products.any? {|product| product.part_number == "part_num_5"} | |
=> true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# With callback methods, if they return false, it will halt the chain and your model won't be saved. | |
# Ninety-nine out of a hundred times, this is only an issue if you're 'caching' a boolean value based on | |
# an association status or other convoluted evaluation | |
class Entity < ActiveRecord::Base | |
named_scope :publicly_available, :conditions => {:publicly_available => true} | |
has_many :items | |
belongs_to :user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# initial idea for state(or step #)-based validation | |
# for the model below, it would only run those validations when instance_model#step == "initial" or nil | |
class ActiveRecord::Base | |
def self.validate_on_step(attribute, &block) | |
attribute, step_attribute = attribute.to_s, "step" | |
self.send :attr_accessor, step_attribute unless self.respond_to?(step_attribute) | |
with_options(:if => lambda {|w| (w.send(step_attribute) == attribute) || (w.send(step_attribute).nil?) }) do |validating_model| | |
yield(validating_model) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
map.namespace :admin do |admin| | |
# users | |
admin.resources :users | |
# product classifications | |
admin.namespace :products do |admin_products| | |
admin_products.resources :product_groups, :has_many => [:product_lines] | |
admin_products.resources :product_lines | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[joshuaclayton ~/Sites/plugins and libraries/github_plugins/mephisto | master]$ rak dispatch_path | |
app/controllers/account_controller.rb | |
26| redirect_back_or_default(dispatch_path) | |
app/controllers/mephisto_controller.rb | |
8| @dispatch_path = Mephisto::Dispatcher.run(site, params[:path].dup) | |
9| @dispatch_action = @dispatch_path.shift | |
10| @section = @dispatch_path.shift | |
18| # @dispatch_path.first has the headers | |
19| if @dispatch_path.first.is_a?(Hash) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source ~/.git-completion.sh | |
PS1='[\u \w$(__git_ps1 " | %s")]\$ ' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is useful if you need to get a subclassed model through a has_many :through association | |
# Just iterate through each user type you want available and create a named_scope. It sounds | |
# repetitive since you can just call Subclass.all to return those results, but if it's a | |
# :through association, it becomes harder to grab | |
# | |
# For example, let's say I've got a handful of different user subclasses, and I've got | |
# magazines and subscriptions. I want to, on a subscription, find all users of a certain | |
# subclass. Adding named scopes to a user is much easier than iterating through the array to | |
# find all users whose type matches whichever. | |
# |
OlderNewer