Class: Statixite::SitesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/statixite/sites_controller.rb

Instance Method Summary collapse

Instance Method Details

#build_and_previewObject



36
37
38
39
# File 'app/controllers/statixite/sites_controller.rb', line 36

def build_and_preview
  @site = Site.find(params[:id])
  build_preview
end

#createObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/controllers/statixite/sites_controller.rb', line 41

def create
  @site = Site.new(site_params)
  result = SiteOperationService.new(@site, { :branch => params[:repo_branch] }).build_template
  if result.successful?
    flash[:success] = "#{@site.statixite_name} saved."
    redirect_to sites_path
  else
    @templates = YAML.load_file Statixite::Engine.root.join("lib", "assets", "templates.yaml")
    @templates = @templates.select { |template| template["whitelisted"].present? && template["whitelisted"] == true }
    if result.error_message.present?
      flash.now[:alert] = result.error_message
    end
    render :new
  end
end

#destroyObject



86
87
88
89
90
91
92
93
94
# File 'app/controllers/statixite/sites_controller.rb', line 86

def destroy
  @site = Site.find(params[:id])
  if SiteDeactivationService.new(@site).deactivate.successful?
    flash[:notice] = "Site Removed!"
  else
    flash[:notice] = "Something went wrong."
  end
  redirect_to sites_path
end

#indexObject



57
58
59
# File 'app/controllers/statixite/sites_controller.rb', line 57

def index
  @sites = Site.all.order(:site_name).page params[:page]
end

#newObject



4
5
6
7
8
# File 'app/controllers/statixite/sites_controller.rb', line 4

def new
  @site = Site.new
  @templates = YAML.load_file Statixite::Engine.root.join("lib", "assets", "templates.yaml")
  @templates = @templates.select { |template| template["whitelisted"].present? && template["whitelisted"] == true }
end

#preview_credentialsObject



96
97
98
99
100
101
102
103
# File 'app/controllers/statixite/sites_controller.rb', line 96

def preview_credentials
  @site = Site.find(params[:id])
  HTAuth::PasswdFile.open(auth_file, HTAuth::File::CREATE) do |pf|
    pf.add_or_update(params[:login], params[:password])
  end
  flash.now[:notice] = "Credentials Saved"
  render :build_and_preview
end

#repo_branchesObject



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

def repo_branches
  @site = Site.new(template_repo: params[:repo])
  @site.build_option = 'custom'
  @site.valid?
  if params[:repo].present? && params[:repo].is_a?(String) && @site.errors[:template_repo].blank?
    begin
      status = Timeout::timeout(20) {
        g = Git.ls_remote(params[:repo])
        @data = g["branches"].keys
        @message = "Repo Found, please choose a branch to initialize the new repository with."
      } 
    rescue Git::GitExecuteError, Timeout::Error => e
      @data = []
      @message = "Git Repository not found"
      @status = 404
    end
  else
    @data = []
    @message = "Git Repository not found"
    @status = 404
  end
  respond_to do |format|
    format.json { render :json => { message: @message, data: @data, status: (@status == 404 ? 'error' : 'success') }, :status => @status }
  end
end

#settingsObject



61
62
63
64
65
66
67
68
# File 'app/controllers/statixite/sites_controller.rb', line 61

def settings
  @site = Site.find(params[:id])
  @settings = @site.settings
  respond_to do |format|
    format.html
    format.json { render :json => @settings }
  end
end

#updateObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/controllers/statixite/sites_controller.rb', line 70

def update
  @site = Site.find(params[:id])
  @site.settings = params[:site][:settings]
  @site.domain_name = params[:site][:domain_name]
  if @site.save
    apply_config_changes
    respond_to do |format|
      format.json { render :json => @site.to_json }
    end
  else
    respond_to do |format|
      format.json { render :json => { :errors => @site.errors }, :status => :unprocessible_entity }
    end
  end
end