class LangArticle < ApplicationRecord
  
  belongs_to :lang_site
  belongs_to :article
  
	validates :title, :presence => true, :uniqueness => {scope: :lang_site_id}
	validates :slug, :presence => true, :uniqueness => {scope: :lang_site_id}
  
  belongs_to :image_file
  belongs_to :slider_image_file, :class_name => "ImageFile"
  
	before_validation do
		self.slug = self.title.to_slug if !self.slug? 
		#self.tags_cache = self.tags_cache.gsub(/ +/, ' ').gsub(/, /,',').gsub(/ ,/,',').gsub(/,+/, ',')
    #self.tags_cache = self.tags_cache.to_s.gsub(/ +/, ' ').gsub(/, /,',').gsub(/ ,/,',').gsub(/,+/, ',')
		
	end
  
  def html
   
    
    
    
    @lang_article = self
    @article =self.article
    @lang = self.lang_site
    
    @search = true
    #view = ActionView::Base.new('app/views/public/articles', {:lang => @lang, :lang_article => self, :article => self.article },  ActionController::Base.new)
    #puts view.render(file: '_show.html.haml')
    render_anywhere 'public/articles/_show', {:search => @search, :lang => @lang, :lang_article => self, :article => self.article, :page_images_credits => [] }
    #ApplicationController.new.render_to_string(
    #  :template => 'public/articles/_show',
    #  :locals =>  {:@lang => @lang, :@lang_article => self, :@article => self.article }
    #)
  end
  
  
  def render_anywhere(partial, assigns= {})
    view = ActionView::Base.new(Rails.configuration.paths['app/views'], assigns)
    view.extend ApplicationHelper
    
    view.class_eval do
      include Rails.application.routes.url_helpers
      include ApplicationHelper
      include IconHelper
      include TranslationHelper
      def protect_against_forgery?
        false
      end
    end
    
    view.render(:template => partial, :layout => false)
    
    
  end
end
