Class: TrustyCms::Setup

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/trusty_cms/setup.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config.



15
16
17
# File 'lib/trusty_cms/setup.rb', line 15

def config
  @config
end

Class Method Details

.bootstrap(config) ⇒ Object



8
9
10
11
12
# File 'lib/trusty_cms/setup.rb', line 8

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

Instance Method Details

#bootstrap(config) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/trusty_cms/setup.rb', line 17

def bootstrap(config)
  @config = config
  @admin = create_admin_user(config[:admin_name], config[:admin_username], config[:admin_password])
  load_default_configuration
  # load_database_template(config[:database_template])
  announce "Finished."
end

#create_admin_user(name, username, password) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/trusty_cms/setup.rb', line 25

def create_admin_user(name, username, password)
  unless name and username and password
    announce "Create the admin user (press enter for defaults)."
    name = prompt_for_admin_name unless name
    username = prompt_for_admin_username unless username
    password = prompt_for_admin_password unless password
  end
  attributes = {
    :name => name,
    :login => username,
    :password => password,
    :password_confirmation => password
  }
  admin = User.find_by(login: username)
  admin = User.new unless admin
  admin.update_attributes(attributes)
  admin.admin = true
  admin.save
  admin
end

#load_database_template(filename) ⇒ Object



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

def load_database_template(filename)
  template = nil
  if filename
    name = find_template_in_path(filename)
    unless name
      announce "Invalid template name: #{filename}"
      filename = nil
    else
      template = load_template_file(name)
    end
  end
  unless filename
    templates = find_and_load_templates("#{TRUSTY_CMS_ROOT}/db/templates/*.yml")
    templates.concat find_and_load_templates("#{TRUSTY_CMS_ROOT}/vendor/extensions/**/db/templates/*.yml")
    TrustyCms::Extension.descendants.each do |d|
      templates.concat find_and_load_templates(d.root + '/db/templates/*.yml')
    end
    templates.concat find_and_load_templates("#{Rails.root}/vendor/extensions/**/db/templates/*.yml")
    templates.concat find_and_load_templates("#{Rails.root}/db/templates/*.yml")
    templates.uniq!
    choose do |menu|
      menu.header = "\nSelect a database template"
      menu.prompt = "[1-#{templates.size}]: "
      menu.select_by = :index
      templates.each { |t| menu.choice(t['name']) { template = t } }
    end
  end
  create_records(template)
end

#load_default_configurationObject



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/trusty_cms/setup.rb', line 46

def load_default_configuration
  feedback "\nInitializing configuration" do
    step { TrustyCms::Config['admin.title'   ] = 'TrustyCms CMS' }
    step { TrustyCms::Config['admin.subtitle'] = 'Publishing for Small Teams' }
    step { TrustyCms::Config['defaults.page.parts' ] = 'body, extended' }
    step { TrustyCms::Config['defaults.page.status' ] = 'Draft' }
    step { TrustyCms::Config['defaults.page.filter' ] = nil }
    step { TrustyCms::Config['defaults.page.fields'] = 'Keywords, Description' }
    step { TrustyCms::Config['session_timeout'] = 2.weeks }
    step { TrustyCms::Config['default_locale'] = 'en' }
  end
end