Advertisement
saasbook

method_with_feature_flags.rb

Apr 2nd, 2012
853
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.80 KB | None | 0 0
  1. class Moviegoer < ActiveRecord::Base
  2.   # here's version n+1, using Setler gem for feature flag:
  3.   scope :old_schema, where :migrated => false
  4.   scope :new_schema, where :migrated => true
  5.   def self.find_matching_names(string)
  6.     if Featureflags.new_name_schema
  7.       Moviegoer.new_schema.where('last_name LIKE :s OR first_name LIKE :s',
  8.         :s => "%#{string}%") +
  9.         Moviegoer.old_schema.where('name like ?', "%#{string}%")
  10.     else # use only old schema
  11.       Moviegoer.where('name like ?', "%#{string}%")
  12.     end
  13.   end
  14.   # automatically update records to new schema when they are saved
  15.   before_save :update_schema, :unless => lambda { |m| m.migrated? }
  16.   def update_schema
  17.     if name =~ /^(.*)\s+(.*)$/
  18.       self.first_name = $1
  19.       self.last_name = $2
  20.     end
  21.     self.migrated = true
  22.   end
  23. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement