Commit c795cd5d4f62d87b488369fda27baf70ba12244d

Authored by juniorsilva
1 parent cbcad870

script-sample-data: fixed script sample-data errors on some records

AI3091
lib/noosfero/sample_data_helper.rb
@@ -11,13 +11,13 @@ module Noosfero::SampleDataHelper @@ -11,13 +11,13 @@ module Noosfero::SampleDataHelper
11 11
12 def save(obj, &block) 12 def save(obj, &block)
13 begin 13 begin
14 - if obj.save  
15 - print '.'  
16 - instance_eval &block if block  
17 - return obj  
18 - else  
19 - print 'F'  
20 - end 14 + if obj.save
  15 + print '.'
  16 + instance_eval &block if block
  17 + return obj
  18 + else
  19 + print 'F'
  20 + end
21 rescue 21 rescue
22 print 'E' 22 print 'E'
23 end 23 end
script/sample-articles
@@ -13,14 +13,16 @@ print "Creating some TinyMce articles: " @@ -13,14 +13,16 @@ print "Creating some TinyMce articles: "
13 for subject in SUBJECTS 13 for subject in SUBJECTS
14 rand(20).times do |i| 14 rand(20).times do |i|
15 profile = profiles.rand 15 profile = profiles.rand
  16 + name = "%s #{subject}" % profile.name
  17 + next if profile.articles.where(:slug => name.to_slug).first
16 article = TinyMceArticle.new( 18 article = TinyMceArticle.new(
17 - :name => "%s #{subject}" % profile.name,  
18 - :body => "%s #{subject}" % profile.name, 19 + :name => name,
  20 + :body => name,
19 :tag_list => [TAGS.rand, TAGS.rand], 21 :tag_list => [TAGS.rand, TAGS.rand],
20 :profile => profile 22 :profile => profile
21 ) 23 )
22 save article do 24 save article do
23 - categories.rand.articles << article 25 + article.add_category categories.rand
24 end 26 end
25 end 27 end
26 end 28 end
@@ -30,9 +32,11 @@ print &quot;Creating some galleries: &quot; @@ -30,9 +32,11 @@ print &quot;Creating some galleries: &quot;
30 for subject in SUBJECTS 32 for subject in SUBJECTS
31 rand(20).times do |i| 33 rand(20).times do |i|
32 profile = profiles.rand 34 profile = profiles.rand
  35 + name = "Gallery %s #{subject}" % profile.name
  36 + next if profile.articles.where(:slug => name.to_slug).first
33 save Gallery.new( 37 save Gallery.new(
34 - :name => "Gallery %s #{subject}" % profile.name,  
35 - :body => "Gallery %s #{subject}" % profile.name, 38 + :name => name,
  39 + :body => name,
36 :tag_list => [TAGS.rand, TAGS.rand], 40 :tag_list => [TAGS.rand, TAGS.rand],
37 :profile => profile 41 :profile => profile
38 ) 42 )
@@ -43,16 +47,20 @@ done @@ -43,16 +47,20 @@ done
43 print "Creating some events: " 47 print "Creating some events: "
44 for subject in EVENT_SUBJECTS 48 for subject in EVENT_SUBJECTS
45 for theme in THEMES 49 for theme in THEMES
  50 + profile = profiles.rand
  51 + name = subject % theme
  52 + next if profile.articles.where(:slug => name.to_slug).first
46 event = Event.new( 53 event = Event.new(
47 - :name => subject % theme,  
48 - :profile => profiles.rand, 54 + :name => name,
  55 + :profile => profile,
49 :start_date => Date.today + (-30 + rand(60)).days, 56 :start_date => Date.today + (-30 + rand(60)).days,
50 :tag_list => [TAGS.rand, TAGS.rand] 57 :tag_list => [TAGS.rand, TAGS.rand]
51 ) 58 )
52 save event do 59 save event do
53 - categories.rand.events << event  
54 - categories.rand.events << event  
55 - categories.rand.events << event 60 + 3.times do
  61 + category = categories.rand
  62 + event.add_category category
  63 + end
56 end 64 end
57 end 65 end
58 end 66 end
script/sample-enterprises
@@ -32,9 +32,12 @@ groups.each do |group| @@ -32,9 +32,12 @@ groups.each do |group|
32 :lat => rand_position(:lat), 32 :lat => rand_position(:lat),
33 :lng => rand_position(:lng) 33 :lng => rand_position(:lng)
34 ) 34 )
  35 + next if Profile[enterprise.identifier]
35 save enterprise do 36 save enterprise do
36 - categories.rand.enterprises << enterprise  
37 - categories.rand.enterprises << enterprise 37 + 2.times do
  38 + category = categories.rand
  39 + enterprise.add_category category
  40 + end
38 end 41 end
39 end 42 end
40 end 43 end
@@ -43,7 +46,6 @@ done @@ -43,7 +46,6 @@ done
43 46
44 EnterpriseActivation.find(:all, :conditions => ['created_at > ?', start_time]).each do |activation| 47 EnterpriseActivation.find(:all, :conditions => ['created_at > ?', start_time]).each do |activation|
45 enterprise = activation.enterprise 48 enterprise = activation.enterprise
46 - puts [activation.code, enterprise.name, enterprise.foundation_year].join(';')  
47 end 49 end
48 50
49 ze = Person['ze'] 51 ze = Person['ze']
script/sample-products
@@ -12,9 +12,11 @@ for thing in THINGS @@ -12,9 +12,11 @@ for thing in THINGS
12 for color in COLORS 12 for color in COLORS
13 name = [color, thing].join(' ') 13 name = [color, thing].join(' ')
14 rand(10).times do |i| 14 rand(10).times do |i|
  15 + profile = enterprises.rand
  16 + next if profile.products.where(:name => name).first
15 save Product.new( 17 save Product.new(
16 :name => name, 18 :name => name,
17 - :profile_id => enterprises.rand.id, :price => (i * 13.7), 19 + :profile_id => profile.id, :price => (i * 13.7),
18 :product_category_id => categories.rand.id 20 :product_category_id => categories.rand.id
19 ) 21 )
20 end 22 end
script/sample-profiles
@@ -63,7 +63,7 @@ for name in NAMES @@ -63,7 +63,7 @@ for name in NAMES
63 if categories.present? 63 if categories.present?
64 2.times do 64 2.times do
65 category = categories.rand 65 category = categories.rand
66 - category.people << user.person unless category.people.include?(user.person) 66 + user.person.add_category category unless category.people.include?(user.person)
67 end 67 end
68 end 68 end
69 end 69 end
@@ -148,7 +148,7 @@ for verb in VERBS @@ -148,7 +148,7 @@ for verb in VERBS
148 if categories.present? 148 if categories.present?
149 2.times do 149 2.times do
150 category = categories.rand 150 category = categories.rand
151 - category.communities << community unless category.communities.include?(community) 151 + community.add_category category unless category.communities.include?(community)
152 end 152 end
153 end 153 end
154 end 154 end