Module: Gluttonberg::Content::Localization::Model::ClassMethods
- Defined in:
- lib/gluttonberg/content/localization.rb
Instance Method Summary collapse
- #is_localized(&blk) ⇒ Object
- #localized? ⇒ Boolean
-
#new_with_localization(attrs = {}) ⇒ Object
Returns a new instance of the model, with a localization instance already assigned to it based on the options passed in.
Instance Method Details
#is_localized(&blk) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/gluttonberg/content/localization.rb', line 22 def is_localized(&blk) # Why yes, this is localized. self.localized = true # Create the localization model class_name = self.name + "Localization" table_name = class_name.tableize # Check to see if the localization is inside a constant target = Object if class_name.index("::") modules = class_name.split("::") # Remove the localization class from the end class_name = modules.pop # Get each constant in turn modules.each { |mod| target = target.const_get(mod) } end self.localized_model = Class.new(ActiveRecord::Base) target.const_set(class_name, self.localized_model) self.localized_model_name = class_name self.localized_model.table_name = table_name # Add the properties declared in the block, and sprinkle in our own mixins self.localized_model.class_eval(&blk) self.localized_model.send(:include, ModelLocalization) # For each property on the localization model, create an accessor on # the parent model, without over-writing any of the existing methods. exclusions = [:id, :created_at, :updated_at, :locale_id , :parent_id] localized_properties = self.localized_model.column_names.reject { |p| exclusions.include? p } non_localized_properties = self.column_names.reject { |p| exclusions.include? p } self.localized_model.attr_accessible :locale_id , :parent_id localized_properties.each do |prop| self.localized_fields << prop # Create the accessor that points to the localized version unless non_localized_properties.include?(prop) class_eval %{ def #{prop} current_localization.#{prop} end def #{prop}=(val) current_localization.#{prop} = val end } end end #---------TODO cleaning html code #@localized_model.clean_html([:text])#(@localized_fields.map{|c|c.to_sym}) # Associate the model and it’s localization has_many :localizations, :class_name => self.localized_model.name.to_s, :foreign_key => :parent_id, :dependent => :destroy has_one :default_localization, :class_name => self.localized_model.name.to_s, :foreign_key => :parent_id , :conditions => ["locale_id = ?", Locale.first_default.id] self.localized_model.belongs_to(:parent, :class_name => self.name, :foreign_key => :parent_id) # Set up validations for when we update in the presence of a localization after_validation :validate_current_localization after_save :save_current_localization end |
#localized? ⇒ Boolean
87 88 89 |
# File 'lib/gluttonberg/content/localization.rb', line 87 def localized? self.localized end |
#new_with_localization(attrs = {}) ⇒ Object
Returns a new instance of the model, with a localization instance already assigned to it based on the options passed in.
The options include the attributes for the both new model and its localization.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/gluttonberg/content/localization.rb', line 95 def new_with_localization(attrs={}) new_model = new default_localization = nil # new localization object for all locales. Gluttonberg::Locale.all.each do |locale| loc = self.localized_model.new(:locale_id => locale.id) new_model.instance_variable_set(:@current_localization, loc) new_model.localizations << loc new_model.attributes = attrs #update current object and current localization if locale.default? default_localization = loc end end # make default localization as default default_localization = new_model.localizations.first if default_localization.blank? new_model.instance_variable_set(:@current_localization, default_localization) new_model end |