forked from activeadmin/activeadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrails_template_with_data.rb
58 lines (47 loc) · 1.79 KB
/
rails_template_with_data.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
# Use the default
apply File.expand_path("../rails_template.rb", __FILE__)
# Register Active Admin controllers
%w{ Post User Category }.each do |type|
generate :'active_admin:resource', type
end
scopes = <<-EOF
scope :all, :default => true
scope :drafts do |posts|
posts.where(["published_at IS NULL"])
end
scope :scheduled do |posts|
posts.where(["posts.published_at IS NOT NULL AND posts.published_at > ?", Time.now.utc])
end
scope :published do |posts|
posts.where(["posts.published_at IS NOT NULL AND posts.published_at < ?", Time.now.utc])
end
scope :my_posts do |posts|
posts.where(:author_id => current_admin_user.id)
end
EOF
inject_into_file 'app/admin/posts.rb', scopes , :after => "ActiveAdmin.register Post do\n"
# Setup some default data
append_file "db/seeds.rb", <<-EOF
users = ["Jimi Hendrix", "Jimmy Page", "Yngwie Malmsteen", "Eric Clapton", "Kirk Hammett"].collect do |name|
first, last = name.split(" ")
User.create! :first_name => first,
:last_name => last,
:username => [first,last].join('-').downcase,
:age => rand(80)
end
categories = ["Rock", "Pop Rock", "Alt-Country", "Blues", "Dub-Step"].collect do |name|
Category.create! :name => name
end
published_at_values = [Time.now.utc - 5.days, Time.now.utc - 1.day, nil, Time.now.utc + 3.days]
1_000.times do |i|
user = users[i % users.size]
cat = categories[i % categories.size]
published_at = published_at_values[i % published_at_values.size]
Post.create :title => "Blog Post \#{i}",
:body => "Blog post \#{i} is written by \#{user.username} about \#{cat.name}",
:category => cat,
:published_at => published_at,
:author => user
end
EOF
rake 'db:seed'