Extract complex logic from InformationTexts#index into separate private methods
This commit is contained in:
@@ -2,22 +2,8 @@ class Admin::SiteCustomization::InformationTextsController < Admin::SiteCustomiz
|
||||
include Translatable
|
||||
|
||||
def index
|
||||
existing_keys = {}
|
||||
@tab = params[:tab] || :debates
|
||||
|
||||
I18nContent.begins_with_key(@tab)
|
||||
.all
|
||||
.map{ |content| existing_keys[content.key] = content }
|
||||
|
||||
@content = {}
|
||||
|
||||
I18n.backend.send(:translations)[:en].each do |k,v|
|
||||
@content[k.to_s] = flat_hash(v).keys
|
||||
.map{ |s| existing_keys["#{k.to_s}.#{s}"].nil? ?
|
||||
I18nContent.new(key: "#{k.to_s}.#{s}") :
|
||||
existing_keys["#{k.to_s}.#{s}"] }
|
||||
end
|
||||
|
||||
fetch_existing_keys
|
||||
append_or_create_keys
|
||||
@content = @content[@tab.to_s]
|
||||
end
|
||||
|
||||
@@ -60,6 +46,26 @@ class Admin::SiteCustomization::InformationTextsController < Admin::SiteCustomiz
|
||||
end
|
||||
end
|
||||
|
||||
def fetch_existing_keys
|
||||
existing_keys = {}
|
||||
@tab = params[:tab] || :debates
|
||||
|
||||
I18nContent.begins_with_key(@tab)
|
||||
.all
|
||||
.map{ |content| existing_keys[content.key] = content }
|
||||
end
|
||||
|
||||
def append_or_create_keys
|
||||
@content = {}
|
||||
|
||||
I18n.backend.send(:translations)[:en].each do |k, v|
|
||||
@content[k.to_s] = flat_hash(v).keys
|
||||
.map{ |s| existing_keys["#{k.to_s}.#{s}"].nil? ?
|
||||
I18nContent.new(key: "#{k.to_s}.#{s}") :
|
||||
existing_keys["#{k.to_s}.#{s}"] }
|
||||
end
|
||||
end
|
||||
|
||||
def flat_hash(h, f = nil, g = {})
|
||||
return g.update({ f => h }) unless h.is_a? Hash
|
||||
h.each { |k, r| flat_hash(r, [f,k].compact.join('.'), g) }
|
||||
|
||||
Reference in New Issue
Block a user