collin (owner)

Revisions

gist: 125369 Download_button fork
public
Public Clone URL: git://gist.github.com/125369.git
Embed All Files: show embed
user.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
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
user_session.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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