Class: Spree::Setup

Inherits:
Object show all
Defined in:
lib/spree/setup.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config.



25
26
27
# File 'lib/spree/setup.rb', line 25

def config
  @config
end

Class Method Details

.bootstrap(config) ⇒ Object



12
13
14
15
16
# File 'lib/spree/setup.rb', line 12

def bootstrap(config)
  setup = new
  setup.bootstrap(config)
  setup
end

.create_admin_userObject



17
18
19
# File 'lib/spree/setup.rb', line 17

def create_admin_user
  new.create_admin_user
end

.load_sample_dataObject



20
21
22
# File 'lib/spree/setup.rb', line 20

def load_sample_data
  new.load_sample_data
end

Instance Method Details

#bootstrap(config) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/spree/setup.rb', line 27

def bootstrap(config)    
  # make sure the product images directory exists
  FileUtils.mkdir_p "#{RAILS_ROOT}/public/images/products/"
  
  @config = config      
  load_default_data unless Country.count > 0
  create_admin_user(config[:admin_password], config[:admin_email]) unless User.first(:include => :roles, :conditions => ["roles.name = 'admin'"])     
  
  if RAILS_ENV == 'production' and Product.count > 0
    announce "WARNING: Running bootstrap in production mode and there is already existing product data.  Sample data will not be loaded."
  else
    load_sample_data if sample_data?
  end
  announce "Bootstrap Complete.\n\n"
end

#create_admin_user(password = nil, email = nil) ⇒ Object



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
# File 'lib/spree/setup.rb', line 43

def create_admin_user(password=nil, email=nil)
  unless email and password
    announce "Create the admin user (press enter for defaults)."
    #name = prompt_for_admin_name unless name
    email = prompt_for_admin_email unless email
    password = prompt_for_admin_password unless password
  end
  attributes = {
    :password => password,
    :password_confirmation => password,
    :email => email,
    :login => email
  }
  
  if User.(email)
    say "\nWARNING: There is already a user with the email: #{email}, so no account changes were made.  If you wish to create an additional admin user, please run rake db:admin:create again with a different email.\n\n"
  else
    admin = User.create(attributes)

    # create an admin role and and assign the admin user to that role
    role = Role.find_or_create_by_name "admin"
    admin.roles << role
    admin.save          
  end      
end

#load_default_dataObject

Loads default data necessary for basic spree functionality



70
71
72
# File 'lib/spree/setup.rb', line 70

def load_default_data
  Rake::Task["db:seed"].invoke
end

#load_sample_dataObject

Uses a special set of fixtures to load sample data



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
# File 'lib/spree/setup.rb', line 75

def load_sample_data
  # load initial database fixtures (in db/sample/*.yml) into the current environment's database
  ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym)
  Dir.glob(File.join(SPREE_ROOT, "db", 'sample', '*.{yml,csv}')).each do |fixture_file|
    Fixtures.create_fixtures("#{SPREE_ROOT}/db/sample", File.basename(fixture_file, '.*'))
  end

  # make product images available to the app
  target = "#{RAILS_ROOT}/public/assets/products/"
  source = "#{SPREE_ROOT}/lib/tasks/sample/products/"

  Find.find(source) do |f|
    # omit hidden directories (SVN, etc.)
    if File.basename(f) =~ /^[.]/
      Find.prune 
      next
    end

    src_path = source + f.sub(source, '')
    target_path = target + f.sub(source, '')

    if File.directory?(f)
      FileUtils.mkdir_p target_path
    else
      FileUtils.cp src_path, target_path
    end
  end
  
  # HACK - need to add all sample users to the 'user' role (can't do this in sample fixtures because user role is seed data)
  user_role = Role.find_by_name "user"
  if user_role
    User.all.each { |u| u.roles << user_role unless u.has_role?("user") } 
  end

  announce "Sample data has been loaded"
end