Created
June 7, 2009 15:13
-
-
Save collin/125369 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 'authlogic' | |
| require 'metaid' | |
| class User | |
| include DataMapper::Resource | |
| attr_accessor :password_confirmation | |
| class << self | |
| def < klass | |
| return true if klass == ::ActiveRecord::Base | |
| super(klass) | |
| end | |
| alias validates_length_of validates_length | |
| alias validates_uniqueness_of validates_is_unique | |
| alias validates_confirmation_of validates_is_confirmed | |
| alias validates_presence_of validates_present | |
| alias validates_format_of validates_format | |
| alias validates_numericality_of validates_is_number | |
| def column_names | |
| properties.map {|property| property.name.to_s } | |
| end | |
| def named_scope name, options={}, &block | |
| block = options if options.is_a? Proc | |
| if block | |
| meta_def name do | |
| all(block.call) | |
| end | |
| else | |
| meta_def name do | |
| all(options) | |
| end | |
| end | |
| end | |
| def define_callbacks *callbacks | |
| callbacks.each do |method_name| | |
| callback = method_name.scan /^(before|after)/ | |
| method = %{ | |
| def #{method_name} method_sym, options={}, &block | |
| puts "Called #{method_name}: \#{method_sym}, \#{options.inspect}" | |
| if block_given? | |
| #{callback} :#{method_name}, method_sym, &block | |
| else | |
| #{callback} :#{method_name} do | |
| if options[:if] | |
| return false unless send(options[:if]) | |
| end | |
| if options[:unless] | |
| return false if send(options[:unless]) | |
| end | |
| send method_sym | |
| end | |
| end | |
| end | |
| } | |
| puts method | |
| instance_eval method | |
| define_method method_name do; end | |
| end | |
| end | |
| end | |
| self.define_callbacks *%w( | |
| before_validation | |
| before_save | |
| after_save | |
| ) | |
| property :id, Serial | |
| property :full_name, String, :length => 1000 | |
| property :twitter_user_name, String, :length => 255 | |
| property :email, String, :index => true, :null => false, :length => 1000 | |
| property :crypted_password, String, :length => 255, :null => false | |
| property :password_salt, String, :length => 255, :null => false | |
| property :persistence_token, String, :length => 255, :index => true, :null => false | |
| property :login_count, Integer, :default => 0, :null => false | |
| property :last_request_at, DateTime | |
| property :last_login_at, DateTime, :index => true | |
| property :current_login_at, DateTime | |
| property :last_login_ip, String | |
| property :current_login_ip, String | |
| property :oauth_token, String, :index => true | |
| property :oauth_secret, String | |
| property :twitter_profile, Object | |
| validates_is_unique :twitter_user_name, :if => :validate_twitter_username? | |
| def validate_twitter_username? | |
| not twitter_user_name.blank? | |
| end | |
| has n, :tweets | |
| property :type, Discriminator | |
| include Authlogic::ActsAsAuthentic::Base | |
| include Authlogic::ActsAsAuthentic::Email | |
| include Authlogic::ActsAsAuthentic::LoggedInStatus | |
| include Authlogic::ActsAsAuthentic::Login | |
| include Authlogic::ActsAsAuthentic::MagicColumns | |
| include Authlogic::ActsAsAuthentic::Password | |
| include Authlogic::ActsAsAuthentic::PerishableToken | |
| include Authlogic::ActsAsAuthentic::PersistenceToken | |
| include Authlogic::ActsAsAuthentic::RestfulAuthentication | |
| include Authlogic::ActsAsAuthentic::SessionMaintenance | |
| include Authlogic::ActsAsAuthentic::SingleAccessToken | |
| include Authlogic::ActsAsAuthentic::ValidationsScope | |
| # def self.db_setup? | |
| # true | |
| # end | |
| def self.find_with_case(field, value, sensitivity = true) | |
| # if sensitivity | |
| # send("find_by_#{field}", value) | |
| # else | |
| # first(:conditions => ["LOWER(#{quoted_table_name}.#{field}) = ?", value.downcase]) | |
| # end | |
| first :email => value.downcase | |
| end | |
| def self.quoted_table_name | |
| "users" | |
| end | |
| def self.default_timezone | |
| :utc | |
| end | |
| def self.primary_key | |
| :id | |
| end | |
| alias changed? dirty? | |
| class << self | |
| alias find_by_id get | |
| end | |
| def to_param | |
| id.to_s | |
| end | |
| def method_missing method_name, *args, &block | |
| name = method_name.to_s | |
| super unless name[/_changed\?$/] | |
| dirty_attributes.include? name.scan /(.*)_changed\?$/ | |
| end | |
| acts_as_authentic do |config| config.instance_eval do | |
| validates_uniqueness_of_email_field_options :scope => :id | |
| end end | |
| def fetch_twitter_credentials | |
| return unless twitter_credentials_blank? | |
| return unless twitter_access_token | |
| credentials = twitter.verify_credentials | |
| credentials.delete :status | |
| credentials | |
| self.twitter_profile = credentials | |
| self.twitter_user_name = credentials["screen_name"] | |
| end | |
| def has_twitter_credentials? | |
| fetch_twitter_credentials | |
| not twitter_credentials_blank? | |
| end | |
| def twitter | |
| @twitter ||= Twitter::Base.new(twitter_access_token) | |
| end | |
| def twitter_access_token | |
| return unless oauth_token and oauth_secret | |
| @twitter_access_token ||= OAuth::AccessToken.new( | |
| TwitterConsumer, oauth_token, oauth_secret | |
| ) | |
| end | |
| def twitter_credentials_blank? | |
| oauth_secret.blank? || oauth_token.blank? || twitter_user_name.blank? | |
| end | |
| def has_no_twitter_credentials? | |
| not has_twitter_credentials? | |
| end | |
| def at_name | |
| return nil unless twitter_user_name | |
| "@#{twitter_user_name}" | |
| end | |
| def profile_image_url | |
| if has_twitter_credentials? | |
| twitter_profile["profile_image_url"] | |
| else | |
| "http://static.twitter.com/images/default_profile_mini.png" | |
| end | |
| end | |
| def profile_url | |
| "http://twitter.com/#{twitter_user_name}" | |
| end | |
| def display_name | |
| @display_name ||= "#{full_name} (#{at_name})" | |
| end | |
| def self.tweeters | |
| all :twitter_user_name.not => nil | |
| end | |
| def self.fetch_tweets | |
| tweeters.each {|user| user.fetch_tweets } | |
| end | |
| def fetch_tweets | |
| search = Twitter::Search.new | |
| search.from twitter_user_name | |
| search.per_page 100 | |
| search.since(tweets.from_twitter.last.tweet_id) if tweets.from_twitter.last | |
| search.each do |tweet| | |
| tweet["user"] = self | |
| tweet = Tweet.create_from_raw tweet | |
| self.tweets << tweet if tweet | |
| end | |
| save | |
| end | |
| end |
This file contains hidden or 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 TeacherSession < Authlogic::Session::Base | |
| private | |
| def search_for_record(*args) | |
| # raise args.inspect | |
| klass.send(*args) | |
| # klass.send(:with_scope, :find => (scope[:find_options] || {})) do | |
| # end | |
| end | |
| def save_record(alternate_record = nil) | |
| r = alternate_record || record | |
| r.save_without_session_maintenance if r && r.changed? | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment