Class: Admin::InstallController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/admin/install_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject



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
# File 'app/controllers/admin/install_controller.rb', line 10

def index
  # TODO: can't we somehow encapsulate all this in a single model instead of juggling with 3 different models?
  params[:section] = params[:section]
  params[:section] ||= { :title => t(:'adva.sites.install.section_default') }
  params[:section][:type] ||= 'Page'

  @site = Site.new(params[:site])
  @section = @site.sections.build(params[:section])
  @user = User.new(params[:user])
  @user.name = @user.first_name_from_email
  @user.verified_at = Time.zone.now

  @article = @section.articles.build({
    title: t(:'adva.sites.install.section_default'),
    body: t(:'adva.sites.install.section_default'),
    author: @user,
    published_at: Time.zone.now,
  })

  if request.post?
    if @site.valid? && @section.valid? && @article.valid? && @user.valid?
      @site.save
      @user.admin = true
      authenticate_user(:email => @user.email, :password => @user.password)

      # default email for site
      @site.email ||= @user.email
      @site.save

      flash.now[:notice] = t(:'adva.sites.flash.install.success')
      render :action => :confirmation
    else
      models = [@site, @section, @article, @user].map { |model| model.class.name unless model.valid? }.compact
      flash.now[:error] = t(:'adva.sites.flash.install.failure', :models => models.join(', '))
    end
  end
end