static_blocks 0.0.5 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/app/controllers/static_blocks/static_blocks_controller.rb +48 -1
- data/app/models/static_blocks/static_block.rb +54 -0
- data/app/views/layouts/static_blocks/application.html.erb +4 -3
- data/app/views/static_blocks/static_blocks/index.html.erb +16 -2
- data/config/routes.rb +8 -1
- data/lib/static_blocks/engine.rb +1 -0
- data/lib/static_blocks/helpers.rb +2 -2
- data/lib/static_blocks/version.rb +1 -1
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/development.log +58703 -0
- data/spec/dummy/log/test.log +188 -0
- data/spec/dummy/tmp/cache/96E/0B0/static_block%3A%3Afoo%3A%3Aen +1 -0
- data/spec/dummy/tmp/cache/96E/880/static_block%3A%3Aen%3A%3Afoo +1 -0
- data/spec/dummy/tmp/cache/97D/2C0/static_block%3A%3Afoo%3A%3Awk +1 -0
- metadata +8 -4
- data/spec/dummy/tmp/cache/769/300/static_block%3A%3Afoo +0 -1
@@ -2,10 +2,57 @@ require_dependency "static_blocks/application_controller"
|
|
2
2
|
|
3
3
|
module StaticBlocks
|
4
4
|
class StaticBlocksController < ApplicationController
|
5
|
+
|
6
|
+
def export
|
7
|
+
t = Time.now.strftime('%Y%m%d%H%M%S')
|
8
|
+
filename = "static-blocks-#{t}.csv"
|
9
|
+
respond_to do |format|
|
10
|
+
format.csv do
|
11
|
+
send_data StaticBlock.to_csv, :filename => filename
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def export_translations
|
17
|
+
t = Time.now.strftime('%Y%m%d%H%M%S')
|
18
|
+
filename = "static-blocks-translations-#{t}.csv"
|
19
|
+
respond_to do |format|
|
20
|
+
format.csv do
|
21
|
+
send_data StaticBlock.translations_to_csv, :filename => filename
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def import
|
27
|
+
if params[:file].nil?
|
28
|
+
redirect_to root_url
|
29
|
+
flash[:error] = "You did not attach a file."
|
30
|
+
elsif params[:file].original_filename.include? 'translations'
|
31
|
+
redirect_to root_url
|
32
|
+
flash[:error] = "Wrong file. You uploaded the translations."
|
33
|
+
else
|
34
|
+
StaticBlock.import(params[:file])
|
35
|
+
redirect_to root_url, notice: "Static Blocks imported"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def import_translations
|
40
|
+
if params[:file].nil?
|
41
|
+
redirect_to root_url
|
42
|
+
flash[:error] = "You did not attach a file."
|
43
|
+
elsif params[:file].original_filename.include? 'translations'
|
44
|
+
StaticBlock.import_translations(params[:file])
|
45
|
+
redirect_to root_url, notice: "Static Block translations imported"
|
46
|
+
else
|
47
|
+
redirect_to root_url
|
48
|
+
flash[:error] = "Wrong file. You uploaded the default static blocks."
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
5
52
|
# GET /static_blocks
|
6
53
|
# GET /static_blocks.json
|
7
54
|
def index
|
8
|
-
@search = StaticBlock.search(params[:q])
|
55
|
+
@search = StaticBlock.order('title asc').search(params[:q])
|
9
56
|
@static_blocks = @search.result.per_page_kaminari(params[:page]).per(10)
|
10
57
|
|
11
58
|
respond_to do |format|
|
@@ -4,6 +4,7 @@ module StaticBlocks
|
|
4
4
|
after_save :clear_cache
|
5
5
|
scope :published, where(:status => 'published')
|
6
6
|
translates :content
|
7
|
+
validates :title, uniqueness: true
|
7
8
|
|
8
9
|
def to_s
|
9
10
|
content
|
@@ -12,5 +13,58 @@ module StaticBlocks
|
|
12
13
|
def clear_cache
|
13
14
|
Rails.cache.delete("static_block::"+title)
|
14
15
|
end
|
16
|
+
|
17
|
+
def self.to_csv(options = {})
|
18
|
+
static_blocks = self.connection.select_all('select * from static_blocks_static_blocks')
|
19
|
+
static_blocks_column_names = static_blocks.first.keys
|
20
|
+
CSV.generate(options) do |csv|
|
21
|
+
csv << static_blocks_column_names
|
22
|
+
static_blocks.each do |s|
|
23
|
+
csv << s.values_at(*static_blocks_column_names)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.translations_to_csv(options = {})
|
29
|
+
translations = self.connection.select_all('select * from static_blocks_static_block_translations')
|
30
|
+
translation_column_names = translations.first.keys
|
31
|
+
CSV.generate(options) do |csv|
|
32
|
+
csv << translation_column_names
|
33
|
+
translations.each do |t|
|
34
|
+
csv << t.values_at(*translation_column_names)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.import(file)
|
40
|
+
CSV.foreach(file.path, headers: true) do |row|
|
41
|
+
static_block = find_by_title(row["title"]) || new
|
42
|
+
static_block.attributes = row.to_hash.slice(*accessible_attributes)
|
43
|
+
static_block.id = row['id']
|
44
|
+
static_block.save!
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.import_translations(file)
|
49
|
+
CSV.foreach(file.path, headers: true) do |row|
|
50
|
+
# find translation
|
51
|
+
raw_sql = "SELECT * FROM static_blocks_static_block_translations WHERE id=#{row['id']}"
|
52
|
+
translation = ActiveRecord::Base.connection.execute(raw_sql)
|
53
|
+
if translation.present?
|
54
|
+
# update existing translation
|
55
|
+
raw_sql = "
|
56
|
+
UPDATE static_blocks_static_block_translations
|
57
|
+
SET static_blocks_static_block_id=%d, locale='%s', content='%s', created_at='%s', updated_at='%s'
|
58
|
+
WHERE id=%d" % [row['static_blocks_static_block_id'], row['locale'], row['content'], row['created_at'], row['updated_at'], row['id']]
|
59
|
+
else
|
60
|
+
# create new translation
|
61
|
+
raw_sql = "
|
62
|
+
INSERT INTO static_blocks_static_block_translations
|
63
|
+
('id', 'static_blocks_static_block_id', 'locale', 'content', 'created_at', 'updated_at') VALUES
|
64
|
+
('%d', '%d', '%s', '%s', '%s', '%s')" % [row['id'], row['static_blocks_static_block_id'], row['locale'], row['content'], row['created_at'], row['updated_at']]
|
65
|
+
end
|
66
|
+
ActiveRecord::Base.connection.execute(raw_sql)
|
67
|
+
end
|
68
|
+
end
|
15
69
|
end
|
16
70
|
end
|
@@ -14,9 +14,10 @@
|
|
14
14
|
<%= link_to "Static Blocks", static_blocks_path, :class => "brand" %>
|
15
15
|
<div class="nav-collapse collapse">
|
16
16
|
<ul class="nav pull-right">
|
17
|
-
<li><%= link_to "List", static_blocks_path %></li>
|
18
|
-
<li><%= link_to "New", new_static_block_path %></li>
|
19
|
-
|
17
|
+
<li><%= link_to "List blocks", static_blocks_path %></li>
|
18
|
+
<li><%= link_to "New block", new_static_block_path %></li>
|
19
|
+
<li><%= link_to "Export", export_static_blocks_path(:format => :csv) %></li>
|
20
|
+
<li><%= link_to "Export translations", export_translations_static_blocks_path(:format => :csv) %></li> </ul>
|
20
21
|
</div><!--/.nav-collapse -->
|
21
22
|
</div>
|
22
23
|
</div>
|
@@ -1,6 +1,7 @@
|
|
1
1
|
<h1>Listing static blocks</h1>
|
2
|
-
|
3
|
-
<div class="
|
2
|
+
<div class="row">
|
3
|
+
<div class="span6">
|
4
|
+
<h2>Search</h2>
|
4
5
|
<%= search_form_for @search do |f| %>
|
5
6
|
<div class="field">
|
6
7
|
<%= f.label :title_cont, "Title contains" %>
|
@@ -17,6 +18,19 @@
|
|
17
18
|
<div class="actions"><%= f.submit "Search" %></div>
|
18
19
|
<% end %>
|
19
20
|
</div>
|
21
|
+
<div class="span6">
|
22
|
+
<h2>Import</h2>
|
23
|
+
<%= form_tag import_static_blocks_path, multipart: true do %>
|
24
|
+
<%= file_field_tag :file %>
|
25
|
+
<%= submit_tag "Import" %>
|
26
|
+
<% end %>
|
27
|
+
<h2>Import Translations</h2>
|
28
|
+
<%= form_tag import_translations_static_blocks_path, multipart: true do %>
|
29
|
+
<%= file_field_tag :file %>
|
30
|
+
<%= submit_tag "Import Translations" %>
|
31
|
+
<% end %>
|
32
|
+
</div>
|
33
|
+
</div>
|
20
34
|
|
21
35
|
<%= paginate @static_blocks, :theme => 'twitter-bootstrap' %>
|
22
36
|
|
data/config/routes.rb
CHANGED
data/lib/static_blocks/engine.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module StaticBlocks
|
2
2
|
module StaticBlocksHelper
|
3
3
|
def static_block_for(name, default = nil)
|
4
|
-
Rails.cache.fetch("static_block::"+name.to_s) do
|
4
|
+
Rails.cache.fetch("static_block::"+I18n.locale.to_s+"::"+name.to_s) do
|
5
5
|
static_block = StaticBlock.published.find_by_title(name.to_s)
|
6
6
|
if static_block
|
7
7
|
static_block.content
|
@@ -12,7 +12,7 @@ module StaticBlocks
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def static_block_published?(name)
|
15
|
-
Rails.cache.fetch("static_block::"+name.to_s) do
|
15
|
+
Rails.cache.fetch("static_block::"+I18n.locale.to_s+"::"+name.to_s) do
|
16
16
|
StaticBlock.published.find_by_title(name.to_s) || false
|
17
17
|
end
|
18
18
|
end
|
Binary file
|
data/spec/dummy/db/test.sqlite3
CHANGED
Binary file
|