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.
@@ -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
- </ul>
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="well">
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
@@ -1,4 +1,11 @@
1
1
  StaticBlocks::Engine.routes.draw do
2
- resources :static_blocks
2
+ resources :static_blocks do
3
+ collection do
4
+ get 'export'
5
+ get 'export_translations'
6
+ post :import
7
+ post :import_translations
8
+ end
9
+ end
3
10
  root to: 'static_blocks#index'
4
11
  end
@@ -6,6 +6,7 @@ require "ransack"
6
6
  require "kaminari"
7
7
  require "bootstrap-kaminari-views"
8
8
  require "globalize3"
9
+ require "csv"
9
10
 
10
11
  module StaticBlocks
11
12
  class Engine < ::Rails::Engine
@@ -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
@@ -1,3 +1,3 @@
1
1
  module StaticBlocks
2
- VERSION = "0.0.5"
2
+ VERSION = "0.1.0"
3
3
  end
Binary file
Binary file