Module: ActiveRecordEncoding::StandardClassMethods

Defined in:
lib/active_record_encoding.rb

Overview

StandardClassMethods defines class methods for inclusion in ActiveRecord::Base in order to provide the user interface for ActiveRecordEncoding.

Instance Method Summary collapse

Instance Method Details

#encoding(new_encoding, options = {}) ⇒ Object

Set both the external_encoding and the internal_encoding values for this model class.

class User < ActiveRecord::Base
  encoding 'UTF-8'    # affect all binary columns
end

When data is retrived from the database, it will be assumed it is encoded in the given format and returned in the same format.

This may also be called with the :for option pointing to one or more specific columns that this call applies to:

class User < ActiveRecord::Base
  encoding 'ISO-8859-1', :for => :comment
  encoding 'ISO-8859-1', :for => [:first_name, :last_name]
end


135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/active_record_encoding.rb', line 135

def encoding (new_encoding, options = {})
  extend ActiveRecordEncoding::ExtendedClassMethods
  include ActiveRecordEncoding::IncludedInstanceMethods

  if attr_names = options[:for]
    [*attr_names].each do |attr_name|
      active_record_encodings[attr_name.to_s] =
          { :ext => new_encoding, :int => new_encoding }
    end
  else
    @active_record_external_encoding = new_encoding
    @active_record_internal_encoding = new_encoding
  end
end

#external_encoding(new_encoding, options = {}) ⇒ Object

Set the external_encoding value for this model class.

class User < ActiveRecord::Base
  external_encoding 'ISO-8859-1'    # affect all binary columns
end

When data is retrieved from the database, it will be assumed it is encoded in the given format.

This may also be called with the :for option pointing to one or more specific columns that this call applies to:

class User < ActiveRecord::Base
  external_encoding 'ISO-8859-1', :for => :comment
  external_encoding 'ISO-8859-1', :for => [:first_name, :last_name]
end


72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/active_record_encoding.rb', line 72

def external_encoding (new_encoding, options = {})
  extend ActiveRecordEncoding::ExtendedClassMethods
  include ActiveRecordEncoding::IncludedInstanceMethods

  if attr_names = options[:for]
    [*attr_names].each do |attr_name|
      active_record_encodings[attr_name.to_s][:ext] = new_encoding
    end
  else
    @active_record_external_encoding = new_encoding
  end
end

#internal_encoding(new_encoding, options = {}) ⇒ Object

Set the internal_encoding value for this model class.

class User < ActiveRecord::Base
  internal_encoding 'UTF-8'   # affect all binary columns
end

When String objects are returned to the user as a result of an ActiveRecord database lookup, they will be in the given format.

This may also be called with the :for option pointing to one or more specific columns that this call applies to:

class User < ActiveRecord::Base
  internal_encoding 'ISO-8859-1', :for => :comment
  internal_encoding 'ISO-8859-1', :for => [:first_name, :last_name]
end


103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/active_record_encoding.rb', line 103

def internal_encoding (new_encoding, options = {})
  extend ActiveRecordEncoding::ExtendedClassMethods
  include ActiveRecordEncoding::IncludedInstanceMethods

  if attr_names = options[:for]
    [*attr_names].each do |attr_name|
      active_record_encodings[attr_name.to_s][:int] = new_encoding
    end
  else
    @active_record_internal_encoding = new_encoding
  end
end