Module: Gluttonberg::Content::Block::ClassMethods

Defined in:
lib/gluttonberg/content/block.rb

Instance Method Summary collapse

Instance Method Details

#all_with_localization(opts) ⇒ Object

Returns all the matching models with the specificed localization loaded.



94
95
96
97
98
99
# File 'lib/gluttonberg/content/block.rb', line 94

def all_with_localization(opts)
  page_localization_id = opts.delete(:page_localization_id)
  results = all(opts)
  results.each { |r| r.load_localization(page_localization_id) }
  results
end

#is_localized(&blk) ⇒ Object

This declaration is used to create properties on a model which need to be localized. It does this by generating a localized class and association.

It also registers the class as being localized.



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
86
# File 'lib/gluttonberg/content/block.rb', line 55

def is_localized(&blk)
  self.localized = true
  self.attr_accessible :section_name

  # Generate the localization model
  class_name = "#{self.name.demodulize}Localization"
  storage_name = "gb_#{class_name.tableize}"
  localized_model = Class.new(ActiveRecord::Base)
  foreign_key = self.name.foreign_key
  localized_model.table_name = storage_name
  Gluttonberg.const_set(class_name, localized_model)

  # Mix in our base set of properties and methods
  localized_model.send(:include, Gluttonberg::Content::BlockLocalization)
  # Generate additional properties from the block passed in
  localized_model.class_eval(&blk)
  # Store the name so we can easily access it without having to look
  # at this parent class
  localized_model.content_type = self.content_type

  # Tell the content module that we are localized
  localized_model.association_name = "#{self.content_type}_localizations"
  Gluttonberg::Content.register_localization( "#{self.content_type}_localizations".to_sym , localized_model)

  # Set up the associations
  has_many :localizations, :class_name => Gluttonberg.const_get(class_name).to_s  , :foreign_key => "#{self.content_type}_id" , :dependent => :destroy
  localized_model.belongs_to(:parent, :class_name => self.name , :foreign_key => "#{self.content_type}_id")

  localized_model.attr_accessible :text, :parent, :page_localization
  localized_model.is_versioned

end

#localized?Boolean

Does this class have an associated localization class.

Returns:

  • (Boolean)


89
90
91
# File 'lib/gluttonberg/content/block.rb', line 89

def localized?
  self.localized
end