linkingpaths (owner)

Revisions

gist: 14780 Download_button fork
public
Public Clone URL: git://gist.github.com/14780.git
Embed All Files: show embed
Text #
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
require 'alea'
require 'faker'
require 'colored'
 
# Ruby 1.9
unless Object.instance_methods.include? "tap"
  class Object
    def tap
      yield self
      self
    end
  end
end
 
def log(text, color=:white)
  puts text.send(color)
end
 
def the_community
  User.count
end
 
def create_user
  User.new.tap do |u|
    u.login = Faker::Internet.user_name
    u.email = Faker::Internet.free_email
    u.password = u.password_confirmation = "700b_only?"
    u.register!
    u.activate!
    log "User ##{u.id} generated. Welcome '#{u.login}'"
  end
end
 
def update_her_profile(user)
  user.profile.first_name = Faker::Name.first_name
  user.profile.last_name = Faker::Name.last_name
  user.profile.website = user.profile.blog = "www." + Faker::Internet.domain_name
  with_random_avatar do |avatar|
    user.profile.icon = avatar
    user.profile.save!
  end
end
 
def with_random_avatar(&block)
  begin
    uri = URI.parse(Faker::Avatar.avatar)
    tmp_file = uri.path[/.*\/(.*)\z/,1]
    open(tmp_file, "wb") { |file|
        file.write(Net::HTTP.get_response(uri).read_body)
    }
    yield File.new(tmp_file)
    File.delete(tmp_file)
  rescue
    log "Unable to get avatar", :red
  end
end
 
def create_a_group(user)
  Group.new.tap do |g|
    g.name = Faker::Company.catch_phrase
    g.description = Faker::Lorem.paragraphs(rand_within(1..4)).join
    g.author = user
    g.private = maybe
    g.moderated = maybe
    g.tag_list.add Faker::Lorem.words(rand_within(0..6))
    g.save!
 
    with_random_avatar do |avatar|
      g.image = avatar
      g.save!
    end
 
    probably{
      g.activate!
    }
  
    log "Group ##{g.id} generated [#{g.state.upcase}]. Topic: '#{g.name}'", :yellow
  end
end
 
def random_user
  User.find :first, :offset => rand_within(0..User.count - 1)
end
 
def this_group_would_be_joined_by(group, number)
  number.times do
    u = random_user
    group.join(u, almost_never)
    group.activate_membership(u) if maybe
  end
  log " └ #{number} users joined it!", :yellow
end
 
def will_have_a_number_of_friends(user, &block)
  friends = yield
  if friends
    friends.times do
      probably {
        user.profile.add_follower(random_user.profile)
      }
      rarely {
        user.profile.add_friend(random_user.profile)
      }
    end
    log " └ #{user.login} has #{friends} friends."
  end
end
 
100.times do
  a_user = create_user
  frequently do
    update_her_profile(a_user)
  end
  will_have_a_number_of_friends(a_user) do
    probably { rand_within(10..50) } || rarely { 20.percent.of(the_community) }
  end
 
  rarely do
    a_group = create_a_group(a_user)
    a_range_of_users = nil
    frequently do
      a_range_of_users = (1..5).percent.of the_community
    end
    almost_never do
      a_range_of_users = (20..30).percent.of the_community
    end
    this_group_would_be_joined_by(a_group, a_range_of_users) if a_range_of_users
  end
end