Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added variables to edit page #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions app/assets/stylesheets/copycat_engine.css
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,19 @@ div.edit h2 {
text-overflow: ellipsis;
}

div.edit .variables table {
width: auto;
}


div.edit .variables table th {
font-weight: bold;
}
div.edit .variables table td,
div.edit .variables table th {
padding: 1em;
border: 1px dotted;
}
div.edit textarea {
width: 99%;
margin-top: 1em;
Expand Down Expand Up @@ -461,3 +474,6 @@ body {
margin-bottom: .5em;
}

.variables {
clear: both;
}
23 changes: 15 additions & 8 deletions app/models/copycat_translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,24 @@ class CopycatTranslation < ActiveRecord::Base

validates :key, :presence => true
validates :locale, :presence => true

attr_accessible :locale, :key, :value

attr_accessible :locale, :key, :value, :options
serialize :options, Hash
RESERVED_KEYS = [:scope, :default, :separator, :resolve, :rescue_format]

def user_parameters
# RESERVED_KEYS from i18n gem
self.options.dup.delete_if {|k| RESERVED_KEYS.include? k}
end

module Serialize

def import_yaml(yaml)
hash = YAML.load(yaml)
hash.each do |locale, data|
hash_flatten(data).each do |key, value|
c = where(key: key, locale: locale).first
c ||= new(key: key, locale: locale)
c ||= new(key: key, locale: locale)
c.value = value
c.save
end
Expand All @@ -31,14 +38,14 @@ def export_yaml
end
hash.to_yaml
end

# {"foo"=>{"a"=>"1", "b"=>"2"}} ----> {"foo.a"=>1, "foo.b"=>2}
def hash_flatten(hash)
result = {}
result = {}
hash.each do |key, value|
if value.is_a? Hash
if value.is_a? Hash
hash_flatten(value).each { |k,v| result["#{key}.#{k}"] = v }
else
else
result[key] = value
end
end
Expand Down
26 changes: 25 additions & 1 deletion app/views/copycat_translations/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
<div class="edit">
<h2><%= @copycat_translation.key%></h2>
<h2><%= @copycat_translation.key %></h2>
<%= button_to "Delete this item", copycat_translation_path(@copycat_translation), :method => :delete, :onclick => "return confirm('Are you sure?');" %>
<%- if @copycat_translation.user_parameters.present? %>
<div class="variables">
<h4>Variables</h4>
<p> You could use the following variables in your inserted text, just using %{variable} and you will obtain its value</p>
<table>
<thead>
<tr>
<th>Variable</th>
<th>Value</th>
<th>Usage</th>
</tr>
</thead>
<tbody>
<%- @copycat_translation.user_parameters.each do |key, value| %>
<tr>
<td><%= key %></td>
<td><%= value %></td>
<td><%= "%{#{key}}" %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<% end %>
<%= form_for @copycat_translation, :url => { :action => "update" } do |f| %>
<%= f.text_area :value, :rows => 12 %>
<%= f.submit "Update" %>
Expand Down
1 change: 1 addition & 0 deletions db/migrate/20120313191745_create_copycat_translations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ def up
t.string :locale
t.string :key
t.text :value
t.text :options
t.timestamps
end
change_table :copycat_translations do |t|
Expand Down
2 changes: 1 addition & 1 deletion lib/copycat/implementation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def lookup(locale, key, scope = [], options = {})
return cct.value if cct
value = super(locale, key, scope, options)
if value.is_a?(String) || value.nil?
CopycatTranslation.create(locale: locale.to_s, key: key.to_s, value: value)
CopycatTranslation.create(locale: locale.to_s, key: key.to_s, value: value, options: options)
end
value
end
Expand Down