49 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Ruby
		
	
	
	
			
		
		
	
	
			49 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Ruby
		
	
	
	
class PagePart
 | 
						|
   
 | 
						|
  include Mongoid::Document
 | 
						|
  include Mongoid::Timestamps
 | 
						|
 | 
						|
  field :name
 | 
						|
  field :content
 | 
						|
  field :kind
 | 
						|
  field :public_r_tag
 | 
						|
  field :public_r_tag_object_id, :default => nil
 | 
						|
  field :public_r_tag_option, :default => nil
 | 
						|
  field :widget_path
 | 
						|
  
 | 
						|
  field :widget_style
 | 
						|
  field :widget_field , :type => Array
 | 
						|
  field :widget_data_count
 | 
						|
  
 | 
						|
  has_one :i18n_variable, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy
 | 
						|
  has_one :title, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy
 | 
						|
 | 
						|
  belongs_to :page
 | 
						|
  belongs_to :module_app
 | 
						|
 | 
						|
  before_save :set_key, :delete_empty_widget_field
 | 
						|
 | 
						|
  def i18n_variable
 | 
						|
    @i18n_variable ||= I18nVariable.first(:conditions => {:key => 'i18n_variable', :language_value_id => self.id, :language_value_type => self.class}) rescue nil
 | 
						|
  end
 | 
						|
 | 
						|
  def title
 | 
						|
    @title ||= I18nVariable.first(:conditions => {:key => 'title', :language_value_id => self.id, :language_value_type => self.class}) rescue nil
 | 
						|
  end
 | 
						|
 | 
						|
  protected
 | 
						|
  
 | 
						|
  def set_key
 | 
						|
    title.key = 'title' if title && (title.key.blank? rescue true)
 | 
						|
    i18n_variable.key = 'i18n_variable' if i18n_variable && (i18n_variable.key.blank? rescue true)
 | 
						|
  end
 | 
						|
  
 | 
						|
  def delete_empty_widget_field
 | 
						|
	
 | 
						|
	if self.widget_field
 | 
						|
      self.widget_field.reject! { |wf| (wf[0].blank? || wf[1].blank?) }
 | 
						|
	end
 | 
						|
    # self.widget_field.delete("") if self.widget_field
 | 
						|
  end
 | 
						|
 | 
						|
end |