Extract complex logic from InformationTexts#index into separate private methods

This commit is contained in:
Angel Perez
2018-07-20 15:23:54 -04:00
parent 26965b43ee
commit f92006b3f3

View File

@@ -2,22 +2,8 @@ class Admin::SiteCustomization::InformationTextsController < Admin::SiteCustomiz
include Translatable include Translatable
def index def index
existing_keys = {} fetch_existing_keys
@tab = params[:tab] || :debates append_or_create_keys
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
@content = @content[@tab.to_s] @content = @content[@tab.to_s]
end end
@@ -60,6 +46,26 @@ class Admin::SiteCustomization::InformationTextsController < Admin::SiteCustomiz
end end
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 = {}) def flat_hash(h, f = nil, g = {})
return g.update({ f => h }) unless h.is_a? Hash return g.update({ f => h }) unless h.is_a? Hash
h.each { |k, r| flat_hash(r, [f,k].compact.join('.'), g) } h.each { |k, r| flat_hash(r, [f,k].compact.join('.'), g) }