Class: StaticBlocks::SnippetsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/static_blocks/snippets_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /snippets POST /snippets.json



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/controllers/static_blocks/snippets_controller.rb', line 109

def create
  @snippet = Snippet.new(params[:snippet])

  respond_to do |format|
    if @snippet.save
      format.html { redirect_to @snippet, notice: 'Snippet was successfully created.' }
      format.json { render json: @snippet, status: :created, location: @snippet }
    else
      format.html { render action: "new" }
      format.json { render json: @snippet.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /snippets/1 DELETE /snippets/1.json



141
142
143
144
145
146
147
148
149
# File 'app/controllers/static_blocks/snippets_controller.rb', line 141

def destroy
  @snippet = Snippet.find(params[:id])
  @snippet.destroy

  respond_to do |format|
    format.html { redirect_to snippets_url }
    format.json { head :no_content }
  end
end

#editObject

GET /snippets/1/edit



103
104
105
# File 'app/controllers/static_blocks/snippets_controller.rb', line 103

def edit
  @snippet = Snippet.find(params[:id])
end

#exportObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/static_blocks/snippets_controller.rb', line 6

def export
  unless Snippet.any?
    flash[:error] = "There are no snippets"
    redirect_to root_url
    return
  end

  t = Time.now.strftime('%Y-%m-%d-%H-%M-%S')
  app_name = Rails.application.class.parent_name
  filename = "#{app_name}-snippets-#{t}.csv"

  respond_to do |format|
    format.csv do
      send_data Snippet.to_csv, :filename => filename
    end
  end
end

#export_translationsObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/static_blocks/snippets_controller.rb', line 24

def export_translations
  unless Snippet.any?
    flash[:error] = "There are no translations"
    redirect_to root_url
    return
  end

  t = Time.now.strftime('%Y-%m-%d-%H-%M-%S')
  app_name = Rails.application.class.parent_name
  filename = "#{app_name}-translations-#{t}.csv"

  respond_to do |format|
    format.csv do
      send_data Snippet.translations_to_csv, :filename => filename
    end
  end
end

#importObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/static_blocks/snippets_controller.rb', line 42

def import
  if params[:file].nil?
    redirect_to root_url
    flash[:error] = "You did not attach a file."
  elsif params[:file].original_filename.include? '-snippets'
    Snippet.import(params[:file])
    redirect_to root_url, notice: "Snippets imported"
  else
    redirect_to root_url
    flash[:error] = "Error. Please upload a valid snippets csv."
  end
end

#import_translationsObject



55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/static_blocks/snippets_controller.rb', line 55

def import_translations
  if params[:file].nil?
    redirect_to root_url
    flash[:error] = "You did not attach a file."
  elsif params[:file].original_filename.include? '-translations'
    Snippet.import_translations(params[:file])
    redirect_to root_url, notice: "Snippet translations imported"
  else
    redirect_to root_url
    flash[:error] = "Error. Please upload a valid static-blocks-translations csv."
  end
end

#indexObject

GET /static_blocks GET /static_blocks.json



70
71
72
73
74
75
76
77
78
# File 'app/controllers/static_blocks/snippets_controller.rb', line 70

def index
  @search = Snippet.order('title asc').search(params[:q])
  @snippets = @search.result(distinct: true).per_page_kaminari(params[:page]).per(10)

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @snippets }
  end
end

#newObject

GET /snippets/new GET /snippets/new.json



93
94
95
96
97
98
99
100
# File 'app/controllers/static_blocks/snippets_controller.rb', line 93

def new
  @snippet = Snippet.new

  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @snippet }
  end
end

#showObject

GET /snippets/1 GET /snippets/1.json



82
83
84
85
86
87
88
89
# File 'app/controllers/static_blocks/snippets_controller.rb', line 82

def show
  @snippet = Snippet.find(params[:id])

  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @snippet }
  end
end

#updateObject

PUT /snippets/1 PUT /snippets/1.json



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/controllers/static_blocks/snippets_controller.rb', line 125

def update
  @snippet = Snippet.find(params[:id])

  respond_to do |format|
    if @snippet.update_attributes(params[:snippet])
      format.html { redirect_to @snippet, notice: 'Static block was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: "edit" }
      format.json { render json: @snippet.errors, status: :unprocessable_entity }
    end
  end
end