Commit 27ab82a73f970d2ca21bc2ed4a56d3be5e626c23
1 parent
3f54dc4f
Exists in
staging
and in
42 other branches
Refactoring sample-data scripts
- creating helpers in Noosfero::SampleDataHelper to save some 'typing' - adding articles and profiles to categories in sample-data scripts - moving Events creation to sample-articles script - echoing 'F' or 'E' in output in case of fail or error on creating
Showing
8 changed files
with
156 additions
and
139 deletions
Show diff stats
... | ... | @@ -0,0 +1,30 @@ |
1 | +module Noosfero::SampleDataHelper | |
2 | + # tourn on autoflush | |
3 | + STDOUT.sync = true | |
4 | + | |
5 | + environment_id = ARGV.first | |
6 | + $environment = unless environment_id.blank? | |
7 | + Environment.find(environment_id) | |
8 | + else | |
9 | + Environment.default || Environment.create!(:name => 'Noosfero', :is_default => true) | |
10 | + end | |
11 | + | |
12 | + def save(obj, &block) | |
13 | + begin | |
14 | + if obj.save | |
15 | + print '.' | |
16 | + instance_eval &block if block | |
17 | + return obj | |
18 | + else | |
19 | + print 'F' | |
20 | + end | |
21 | + rescue | |
22 | + print 'E' | |
23 | + end | |
24 | + return nil | |
25 | + end | |
26 | + | |
27 | + def done | |
28 | + puts ' done!' | |
29 | + end | |
30 | +end | ... | ... |
script/sample-articles
1 | 1 | #!/usr/bin/env ruby |
2 | 2 | require File.dirname(__FILE__) + '/../config/environment' |
3 | +include Noosfero::SampleDataHelper | |
3 | 4 | |
4 | -# tourn on autoflush | |
5 | -STDOUT.sync = true | |
6 | - | |
7 | -profiles = Profile.all | |
5 | +profiles = $environment.profiles | |
6 | +categories = $environment.categories | |
8 | 7 | SUBJECTS = ['got a new car', 'release a new version of project X', "doesn't like wales no more", "doesn't use free-software no more"] |
9 | 8 | TAGS = ['free-software', 'noosfero', 'development', 'rails', 'ruby'] |
9 | +EVENT_SUBJECTS = ['International Conference on %s', '%s day', '%s World Congress', '%s World Forum', '%s Summit', '%s Week'] | |
10 | +THEMES = ['Sustainability', 'Free Software', 'Climate Change', 'Environment', 'Agile Development', 'Solidarity Economy', 'Debian', 'Perl'] | |
10 | 11 | |
11 | 12 | print "Creating some TinyMce articles: " |
12 | 13 | for subject in SUBJECTS |
13 | 14 | rand(20).times do |i| |
14 | 15 | profile = profiles.rand |
15 | - profile.articles << TinyMceArticle.new( | |
16 | + article = TinyMceArticle.new( | |
16 | 17 | :name => "%s #{subject}" % profile.name, |
17 | 18 | :body => "%s #{subject}" % profile.name, |
18 | - :tag_list => [TAGS.rand, TAGS.rand] | |
19 | + :tag_list => [TAGS.rand, TAGS.rand], | |
20 | + :profile => profile | |
19 | 21 | ) |
20 | - print '.' | |
22 | + save article do | |
23 | + categories.rand.articles << article | |
24 | + end | |
21 | 25 | end |
22 | 26 | end |
23 | -puts ' done!' | |
27 | +done | |
24 | 28 | |
25 | 29 | print "Creating some galleries: " |
26 | 30 | for subject in SUBJECTS |
27 | 31 | rand(20).times do |i| |
28 | 32 | profile = profiles.rand |
29 | - profile.articles << Gallery.new( | |
33 | + save Gallery.new( | |
30 | 34 | :name => "Gallery %s #{subject}" % profile.name, |
31 | 35 | :body => "Gallery %s #{subject}" % profile.name, |
36 | + :tag_list => [TAGS.rand, TAGS.rand], | |
37 | + :profile => profile | |
38 | + ) | |
39 | + end | |
40 | +end | |
41 | +done | |
42 | + | |
43 | +print "Creating some events: " | |
44 | +for subject in EVENT_SUBJECTS | |
45 | + for theme in THEMES | |
46 | + event = Event.new( | |
47 | + :name => subject % theme, | |
48 | + :profile => profiles.rand, | |
49 | + :start_date => Date.today + (-30 + rand(60)).days, | |
32 | 50 | :tag_list => [TAGS.rand, TAGS.rand] |
33 | 51 | ) |
34 | - print '.' | |
52 | + save event do | |
53 | + categories.rand.events << event | |
54 | + categories.rand.events << event | |
55 | + categories.rand.events << event | |
56 | + end | |
35 | 57 | end |
36 | 58 | end |
37 | -puts ' done!' | |
59 | +done | ... | ... |
script/sample-categories
1 | 1 | #!/usr/bin/env ruby |
2 | 2 | require File.dirname(__FILE__) + '/../config/environment' |
3 | - | |
4 | -# turn on autoflush | |
5 | -STDOUT.sync = true | |
6 | - | |
7 | -$environment_default = Environment.default | |
3 | +include Noosfero::SampleDataHelper | |
8 | 4 | |
9 | 5 | def new_category(parent, name, color = nil) |
10 | - print '.' | |
11 | - $environment_default.categories.create(:name => name, :display_color => color, :parent_id => (parent ? parent.id: nil), :display_in_menu => true) | |
6 | + save $environment.categories.build(:name => name, :display_color => color, :parent_id => (parent ? parent.id: nil), :display_in_menu => true) | |
12 | 7 | end |
13 | 8 | |
14 | 9 | def new_region(parent, name, color = nil) |
15 | - print '.' | |
16 | - $environment_default.regions.create(:name => name, :display_color => color, :parent_id => (parent ? parent.id: nil), :display_in_menu => true) | |
10 | + save $environment.regions.build(:name => name, :display_color => color, :parent_id => (parent ? parent.id: nil), :display_in_menu => true) | |
17 | 11 | end |
18 | 12 | |
19 | 13 | def new_state(parent, name) |
20 | - print '.' | |
21 | - State.create(:name => name, :parent => parent, :environment => $environment_default) | |
14 | + save State.new(:name => name, :parent => parent, :environment => $environment) | |
22 | 15 | end |
23 | 16 | |
24 | 17 | def new_productcategory(parent, name) |
25 | - print '.' | |
26 | - ProductCategory.create(:name => name, :environment => $environment_default, :parent => parent) | |
27 | -end | |
28 | - | |
29 | -def done | |
30 | - puts ' done!' | |
18 | + save ProductCategory.new(:name => name, :environment => $environment, :parent => parent) | |
31 | 19 | end |
32 | 20 | |
33 | 21 | print "Creating categories: " | ... | ... |
script/sample-data
1 | 1 | #!/usr/bin/env ruby |
2 | 2 | require File.dirname(__FILE__) + '/../config/environment' |
3 | +include Noosfero::SampleDataHelper | |
3 | 4 | |
4 | -# turn on autoflush | |
5 | -STDOUT.sync = true | |
6 | - | |
7 | -environment_id = ARGV.first | |
8 | -environment = nil | |
9 | -if environment_id | |
10 | - environment = Environment.find(environment_id) | |
11 | -else | |
12 | - environment = Environment.default || Environment.create!(:name => 'Noosfero', :is_default => true) | |
13 | -end | |
14 | - | |
15 | -system('./script/sample-profiles') | |
16 | -system('./script/sample-categories') | |
17 | -system('./script/sample-enterprises') | |
18 | -system('./script/sample-products') | |
19 | -system('./script/sample-articles') | |
5 | +id = ARGV.first || '' | |
6 | +system('./script/sample-categories', id) | |
7 | +system('./script/sample-profiles', id) | |
8 | +system('./script/sample-enterprises', id) | |
9 | +system('./script/sample-products', id) | |
10 | +system('./script/sample-articles', id) | ... | ... |
script/sample-enterprises
1 | 1 | #!/usr/bin/env ruby |
2 | 2 | require File.dirname(__FILE__) + '/../config/environment' |
3 | +include Noosfero::SampleDataHelper | |
3 | 4 | |
4 | -# turn on autoflush | |
5 | -STDOUT.sync = true | |
5 | +categories = $environment.categories | |
6 | 6 | |
7 | 7 | def rand_position(type) |
8 | 8 | range = { |
... | ... | @@ -23,8 +23,8 @@ print "Creating enterprises: " |
23 | 23 | groups.each do |group| |
24 | 24 | what.each do |production| |
25 | 25 | places.each do |place| |
26 | - name = [group, production, place].join(' ') | |
27 | - Enterprise.create!( | |
26 | + name = [group, production, place].join(' ') + " - #{$environment.name}" | |
27 | + enterprise = Enterprise.new( | |
28 | 28 | :name => name, |
29 | 29 | :identifier => name.to_slug, |
30 | 30 | :enabled => false, |
... | ... | @@ -32,11 +32,14 @@ groups.each do |group| |
32 | 32 | :lat => rand_position(:lat), |
33 | 33 | :lng => rand_position(:lng) |
34 | 34 | ) |
35 | - print "." | |
35 | + save enterprise do | |
36 | + categories.rand.enterprises << enterprise | |
37 | + categories.rand.enterprises << enterprise | |
38 | + end | |
36 | 39 | end |
37 | 40 | end |
38 | 41 | end |
39 | -puts " done!" | |
42 | +done | |
40 | 43 | |
41 | 44 | EnterpriseActivation.find(:all, :conditions => ['created_at > ?', start_time]).each do |activation| |
42 | 45 | enterprise = activation.enterprise |
... | ... | @@ -45,6 +48,6 @@ end |
45 | 48 | |
46 | 49 | ze = Person['ze'] |
47 | 50 | # give admin rights for 'ze' in some enterprises |
48 | -Enterprise.all.rand.add_admin(ze) | |
49 | -Enterprise.all.rand.add_admin(ze) | |
50 | -Enterprise.all.rand.add_admin(ze) | |
51 | +$environment.enterprises.rand.add_admin(ze) | |
52 | +$environment.enterprises.rand.add_admin(ze) | |
53 | +$environment.enterprises.rand.add_admin(ze) | ... | ... |
script/sample-products
1 | 1 | #!/usr/bin/env ruby |
2 | 2 | require File.dirname(__FILE__) + '/../config/environment' |
3 | +include Noosfero::SampleDataHelper | |
3 | 4 | |
4 | -# tourn on autoflush | |
5 | -STDOUT.sync = true | |
6 | - | |
7 | -enterprises = Enterprise.all | |
8 | -categories = ProductCategory.all | |
5 | +enterprises = $environment.enterprises | |
6 | +categories = $environment.product_categories | |
9 | 7 | |
10 | 8 | print "Creating products: " |
11 | 9 | THINGS = %w[ Car House Bicycle Book Pen Computer Webcam ] |
... | ... | @@ -14,35 +12,29 @@ for thing in THINGS |
14 | 12 | for color in COLORS |
15 | 13 | name = [color, thing].join(' ') |
16 | 14 | rand(10).times do |i| |
17 | - Product.create( | |
15 | + save Product.new( | |
18 | 16 | :name => name, |
19 | 17 | :enterprise_id => enterprises.rand.id, :price => (i * 13.7), |
20 | 18 | :product_category_id => categories.rand.id |
21 | 19 | ) |
22 | - print '.' | |
23 | 20 | end |
24 | 21 | end |
25 | 22 | end |
26 | -puts ' done!' | |
27 | - | |
28 | -environment = Environment.default | |
23 | +done | |
29 | 24 | |
30 | 25 | print "Creating qualifiers and certifier: " |
31 | 26 | QUALIFIERS = ['Organic', 'Free as in Freedom', 'Regional'] |
32 | 27 | CERTIFIERS = ['FBES', 'Colivre', 'Institute Paulo Freire', 'Fora do Eixo'] |
33 | 28 | for qualifier in QUALIFIERS |
34 | - environment.qualifiers << Qualifier.new(:name => qualifier) | |
35 | - print '.' | |
29 | + save Qualifier.new(:name => qualifier, :environment => $environment) | |
36 | 30 | end |
37 | 31 | for certifier in CERTIFIERS |
38 | - environment.certifiers << Certifier.new(:name => certifier) | |
39 | - print '.' | |
32 | + save Certifier.new(:name => certifier, :environment => $environment) | |
40 | 33 | end |
41 | -puts ' done!' | |
34 | +done | |
42 | 35 | |
43 | 36 | print "Creating units: " |
44 | 37 | [['Litre', 'Litres'], ['Kilo', 'Kilos'], ['Meter', 'Meters']].each do |unit| |
45 | - Unit.create!(:singular => unit[0], :plural => unit[1], :environment => environment) | |
46 | - print '.' | |
38 | + save Unit.new(:singular => unit[0], :plural => unit[1], :environment => $environment) | |
47 | 39 | end |
48 | -puts ' done!' | |
40 | +done | ... | ... |
script/sample-profiles
1 | 1 | #!/usr/bin/env ruby |
2 | 2 | require File.dirname(__FILE__) + '/../config/environment' |
3 | +include Noosfero::SampleDataHelper | |
3 | 4 | |
4 | -# turn on autoflush | |
5 | -STDOUT.sync = true | |
6 | - | |
7 | -environment = Environment.default | |
5 | +categories = $environment.categories | |
8 | 6 | |
9 | 7 | places = [ |
10 | 8 | { :country=>'BR', :city=>'Salvador', |
... | ... | @@ -42,37 +40,63 @@ print "Creating users: " |
42 | 40 | for name in NAMES |
43 | 41 | for surname in SURNAMES |
44 | 42 | full_name = [name, surname].join(' ') |
45 | - user = User.create!({ | |
43 | + user = User.new({ | |
46 | 44 | :login => full_name.to_slug, |
47 | 45 | :email => full_name.to_slug + '@localhost.localdomain', |
48 | 46 | :password => 'test', |
49 | 47 | :password_confirmation => 'test', |
50 | - :environment => environment, | |
48 | + :environment => $environment, | |
51 | 49 | }) |
52 | - user.person.name = full_name | |
53 | - place = places[rand(places.length)] | |
54 | - user.person.data[:country] = place[:country] | |
55 | - user.person.city = place[:city] | |
56 | - user.person.lat = place[:lat] + (rand/100)-0.005 | |
57 | - user.person.lng = place[:lng] + (rand/100)-0.005 | |
58 | - user.person.save! | |
59 | - people << user.person | |
60 | - | |
61 | - print '.' | |
50 | + save user do | |
51 | + user.person.name = full_name | |
52 | + place = places[rand(places.length)] | |
53 | + user.person.data[:country] = place[:country] | |
54 | + user.person.city = place[:city] | |
55 | + user.person.lat = place[:lat] + (rand/100)-0.005 | |
56 | + user.person.lng = place[:lng] + (rand/100)-0.005 | |
57 | + user.person.save! | |
58 | + categories.rand.people << user.person | |
59 | + categories.rand.people << user.person | |
60 | + end | |
62 | 61 | end |
63 | 62 | end |
64 | -puts ' done!' | |
63 | +ze = User.new({ | |
64 | + :login => "ze", | |
65 | + :email => 'root@localhost.localdomain', | |
66 | + :password => 'test', | |
67 | + :password_confirmation => 'test', | |
68 | + :environment => $environment, | |
69 | +}) | |
70 | +save ze do | |
71 | + $environment.add_admin(ze.person) | |
72 | +end | |
73 | + | |
74 | +admin = User.new({ | |
75 | + :login => "adminuser", | |
76 | + :email => 'adminuser@localhost.localdomain', | |
77 | + :password => 'admin', | |
78 | + :password_confirmation => 'admin', | |
79 | + :environment => $environment, | |
80 | +}) | |
81 | +save admin do | |
82 | + $environment.add_admin(admin.person) | |
83 | +end | |
84 | +done | |
65 | 85 | |
86 | +people = $environment.people | |
66 | 87 | print "Creating some friendships: " |
67 | 88 | rand(people.size * 3).times do |
68 | 89 | from = people.rand |
69 | 90 | to = people.rand |
70 | 91 | if from != to && !from.friends.include?(to) |
71 | - AddFriend.create!(:requestor => to, :target => from).finish | |
92 | + task = AddFriend.new(:requestor => to, :target => from) | |
93 | + save task do | |
94 | + task.finish | |
95 | + end | |
72 | 96 | end |
73 | 97 | print '.' |
74 | 98 | end |
75 | -puts ' done!' | |
99 | +done | |
76 | 100 | |
77 | 101 | |
78 | 102 | communities = [] |
... | ... | @@ -82,41 +106,15 @@ print "Creating communities: " |
82 | 106 | for verb in VERBS |
83 | 107 | for stuff in STUFF |
84 | 108 | name = [verb, stuff].join(' ') |
85 | - community = Community.create!(:name => name, :environment => environment) | |
86 | - communities << community | |
87 | - rand(10).times do | |
88 | - community.add_member(people.rand) | |
109 | + community = Community.new(:name => name, :environment => $environment) | |
110 | + save community do | |
111 | + communities << community | |
112 | + rand(10).times do | |
113 | + community.add_member(people.rand) | |
114 | + end | |
115 | + categories.rand.communities << community | |
116 | + categories.rand.communities << community | |
89 | 117 | end |
90 | - print '.' | |
91 | - end | |
92 | -end | |
93 | -puts ' done!' | |
94 | - | |
95 | -EVENTS = ['International Conference on %s', '%s day', '%s World Congress', '%s World Forum', '%s Summit', '%s Week'] | |
96 | -THEMES = ['Sustainability', 'Free Software', 'Climate Change', 'Environment', 'Agile Development', 'Solidarity Economy'] | |
97 | -print "Creating some events: " | |
98 | -for event in EVENTS | |
99 | - for theme in THEMES | |
100 | - Event.create!(:name => event % theme, :profile => communities.rand, :start_date => Date.today + (-30 + rand(60)).days) | |
101 | - print '.' | |
102 | 118 | end |
103 | 119 | end |
104 | -puts ' done!' | |
105 | - | |
106 | -ze = User.create!({ | |
107 | - :login => "ze", | |
108 | - :email => 'root@localhost.localdomain', | |
109 | - :password => 'test', | |
110 | - :password_confirmation => 'test', | |
111 | - :environment => environment, | |
112 | -}).person | |
113 | -environment.add_admin(ze) | |
114 | - | |
115 | -admin = User.create!({ | |
116 | - :login => "adminuser", | |
117 | - :email => 'adminuser@localhost.localdomain', | |
118 | - :password => 'admin', | |
119 | - :password_confirmation => 'admin', | |
120 | - :environment => environment, | |
121 | -}).person | |
122 | -environment.add_admin(admin) | |
120 | +done | ... | ... |
script/sample-qualifiers
1 | 1 | #!/usr/bin/env ruby |
2 | 2 | require File.dirname(__FILE__) + '/../config/environment' |
3 | - | |
4 | -# turn on autoflush | |
5 | -STDOUT.sync = true | |
3 | +include Noosfero::SampleDataHelper | |
6 | 4 | |
7 | 5 | QUALIFIERS = ['Organic', 'Ecological', 'Biodynamic farming', 'Sustainable', 'Agroecological', 'Craft', 'Free as in freedom'] |
8 | 6 | CERTIFIERS = ['FBES', 'Colivre', 'Circuito Fora do Eixo', 'Instituto Paulo Freire', 'Free Software Foundation', 'Linux Foundation', 'GNU', 'Perl Foundation'] |
9 | 7 | |
10 | -environment = Environment.default | |
11 | - | |
12 | 8 | printf "Creating qualifiers and certifiers: " |
13 | 9 | CERTIFIERS.each do |certifier_name| |
14 | - Certifier.create!(:name => certifier_name, :environment => environment) | |
15 | - print '.' | |
10 | + save Certifier.new(:name => certifier_name, :environment => $environment) | |
16 | 11 | end |
17 | 12 | |
18 | 13 | QUALIFIERS.each do |qualifier_name| |
19 | - Qualifier.create!(:name => qualifier_name, :environment => environment) | |
20 | - print '.' | |
14 | + save Qualifier.new(:name => qualifier_name, :environment => $environment) | |
21 | 15 | end |
22 | 16 | |
23 | -Qualifier.all.each do |qualifier| | |
24 | - Certifier.all.sort_by{rand}.slice(0, CERTIFIERS.size / 2).each do |certifier| | |
25 | - QualifierCertifier.create!(:qualifier => qualifier, :certifier => certifier) | |
26 | - print '.' | |
17 | +$environment.qualifiers.each do |qualifier| | |
18 | + $environment.certifiers.sort_by{rand}.slice(0, CERTIFIERS.size / 2).each do |certifier| | |
19 | + save QualifierCertifier.new(:qualifier => qualifier, :certifier => certifier) | |
27 | 20 | end |
28 | 21 | end |
29 | -puts " done!" | |
22 | +done | ... | ... |