Class: Statixite::TemplatesController

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

Instance Method Summary collapse

Instance Method Details

#destroyObject



74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/statixite/templates_controller.rb', line 74

def destroy
  if Dir.glob(File.join(site_path, "**", "*"), File::FNM_DOTMATCH).delete_if { |a| File.basename(a) == "." }.include? old_file
    FileUtils.rm_rf(old_file, :secure => true)
    flash[:notice] = "#{@site.statixite_name}#{old_path} deleted!"
  else
    flash[:alert] = "Something went wrong, try again"
  end
  apply_post_changes
  redirect_to site_template_path
end

#editObject



5
6
7
8
9
10
11
12
13
# File 'app/controllers/statixite/templates_controller.rb', line 5

def edit
  respond_to do |format|
    format.json do
      @template = recurse_site(site_path)
      render :json => @template
    end
    format.html
  end
end

#updateObject



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
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/statixite/templates_controller.rb', line 15

def update
  if template_params[:path] == ""
    @json = { message: "Something went wrong", status: "error" }
    @response_status = 422
  elsif !sanitized?
    @json = { message: "Invalid name. try #{new_name}", status: "error" }
    @response_status = 422
  elsif (rename_folder? || rename_file?) && rename_not_changed? 
    @json = { message: "#{new_name} not changed.", status: 'info' }
    @response_status = 200
  elsif (new_file? || rename_file?) && new_file_or_folder_exists?
    @json = { message: "File already exists. try another name!", status: "error" }
    @response_status = 422
  elsif (new_folder? || rename_folder?) && new_file_or_folder_exists?
    @json = { message: "Folder already exists. try another name!", status: "warn" }
    @response_status = 422
  elsif new_file? && !new_file_or_folder_exists?
    GitService.new(@site.site_clone_path, @site.site_remote).make_changes do
      File.open(new_file, 'w+') { |f| f.write(template_params[:content]) }
    end
    @json = { message: "#{new_name} created!", status: "success", template: recurse_site(site_path)  }
    @response_status = 201
  elsif new_folder? && !new_file_or_folder_exists?
    GitService.new(@site.site_clone_path, @site.site_remote).make_changes do
      Dir.mkdir(new_file)
    end
    @json = { message: "#{new_name} created!", status: "success", template: recurse_site(site_path) }
    @response_status = 201
  elsif (rename_folder? || rename_file?) && !new_file_or_folder_exists? 
    GitService.new(@site.site_clone_path, @site.site_remote).make_changes do
      File.rename(old_file, new_file)
    end
    @json = { message: "Renamed to #{new_name}", status: 'success', template: recurse_site(site_path) }
    @response_status = 201
  elsif edit_file_content?
    GitService.new(@site.site_clone_path, @site.site_remote).make_changes do
      write_file_content
    end
  else
    @json = { message: 'Something went wrong', status: 'error' }
    @response_status = 422
  end
  apply_post_changes if @response_status.to_s[0] == "2"
  render :json => @json, :status => @response_status
end

#upload_filesObject



61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/controllers/statixite/templates_controller.rb', line 61

def upload_files
  if old_file_or_folder_exists? && is_folder?
    GitService.new(@site.site_clone_path, @site.site_remote).make_changes do
      save_uploaded_files
    end
    flash[:notice] = 'Files saved!'
  else
    flash[:alert] =  'Something went wrong, try again.'
  end
  apply_post_changes
  redirect_to site_template_path(@site)
end