Permalink
Browse files

Fix news import

  • Loading branch information...
leo-souza committed Aug 14, 2018
1 parent b1b9f78 commit ed677dc36f05f95a8bbe23657827c9750777fdec
@@ -18,8 +18,8 @@ def generate
s = current_site
h = { include: {} }
h[:include][:pages] = { include: [:i18ns, :related_files] } if params[:pages]
h[:include][:own_news] = { include: [:i18ns, :related_files ] } if params[:news]
h[:include][:news_sites] = { include: [:categories ] } if params[:news]
h[:include][:own_news] = { include: { i18ns: {}, related_files: {}, own_news_site: {include: :categories} } } if params[:news]
#h[:include][:news_sites] = { include: [:categories ] } if params[:news]
h[:include][:events] = { include: [:categories, :i18ns, :related_files] } if params[:events]
h[:include][:repositories] = {} if params[:repositories]
h[:include][:banners] = { include: :categories } if params[:banners]
@@ -72,12 +72,12 @@ def import
uploaded_io = params[:upload]
Import::Application::CONVAR["repository"] = {}
Import::Application::CONVAR["menu"] = {}
case uploaded_io.content_type
when 'text/xml'
parser = Nori.new
attrs = parser.parse(uploaded_io.read)
when 'application/json', 'application/octet-stream'
attrs = JSON.parse uploaded_io.read
case uploaded_io.original_filename
when /\.xml/
parser = Nori.new
attrs = parser.parse(uploaded_io.read)
when /\.json/
attrs = JSON.parse uploaded_io.read
end
attrs = attrs['site'] if attrs['site']
@@ -87,7 +87,7 @@ def import
current_site.banners.import(attrs['banners'], user: current_user.id) if attrs['banners']
current_site.pages.import(attrs['pages'], user: current_user.id, site_id: current_site.id) if attrs['pages']
current_site.own_news.import(attrs['own_news'], user: current_user.id, site_id: current_site.id) if attrs['own_news']
current_site.news_sites.import(attrs['news_sites'], user: current_user.id, site_id: current_site.id) if attrs['own_news']
#current_site.news_sites.import(attrs['news_sites'], user: current_user.id, site_id: current_site.id) if attrs['own_news']
current_site.events.import(attrs['events'], user: current_user.id, site_id: current_site.id) if attrs['events']
current_site.menus.import(attrs['menus']) if attrs['menus']
#current_site.components.import(attrs['root_components'], site_id: current_site.id) if attrs['root_components']
@@ -16,9 +16,12 @@ class News < ActiveRecord::Base
has_many :posts_repositories, as: :post, dependent: :destroy
has_many :related_files, through: :posts_repositories, source: :repository
has_many :news_sites, foreign_key: :journal_news_id, class_name: "::Journal::NewsSite", dependent: :destroy
has_many :sites, :through => :news_sites, class_name: "::Journal::NewsSite"
has_many :sites, :through => :news_sites
has_many :newsletter_histories, dependent: :destroy, class_name: "::Journal::NewsletterHistories"
has_one :own_news_site, ->(this){ where(site_id: this.site_id) }, class_name: "::Journal::NewsSite", foreign_key: :journal_news_id
# Validations
validates :user_id, :site_id, :status, presence: true
@@ -91,6 +94,13 @@ def self.import(attrs, options = {})
self::I18ns.new(i18n.except('id', '@type', 'type', 'created_at', 'updated_at', 'journal_news_id'))
end
news_site_attrs = attrs.fetch('own_news_site', {}).except('id', 'type', '@type', 'journal_news_id', 'created_at', 'updated_at', 'site_id').merge({
site_id: options[:site_id]
})
news_site_attrs['category_list'] = news_site_attrs.delete('categories').to_a.map { |category| category['name'] }
attrs['own_news_site'] = Journal::NewsSite.new(news_site_attrs)
attrs['related_file_ids'] = attrs.delete('related_files').to_a.map {|repo| Import::Application::CONVAR["repository"]["#{repo['id']}"] }
news = self.create!(attrs)

0 comments on commit ed677dc

Please sign in to comment.