module EasyModel::ColumnForActiveRecord::ClassMethods
Public Instance Methods
Protected Instance Methods
column(name, type, options={})
click to toggle source
テーブルに存在しないカラムを定義する.
引数¶ ↑
- name
-
カラム名.
- type
-
データ型.
- options [:default]
-
デフォルト値. 省略した場合は nil.
戻り値¶ ↑
定義したカラムを表す ActiveRecord::ConnectionAdapters::Column オブジェクト.
# File lib/easy_model/column_for_active_record.rb, line 66 def column(name, type, options={}) cast_type = "ActiveRecord::Type::#{type.to_s.capitalize}" cast_type = "ActiveRecord::Type::DateTime" if type.to_s == "datetime" cast_type = "ActiveRecord::Type::Time" if type.to_s == "timestamp" (@easy_model_attribute_names ||= []) << name.to_s ActiveRecord::ConnectionAdapters::Column.new(name, options[:default], cast_type.classify.constantize.new, type, true).tap do |column| define_method("#{name}=") do |value| value = nil if column.number? and value.kind_of?(String) and value.blank? return if value == send(name) instance_variable_set("@#{name}_before_type_cast", value) instance_variable_set("@#{name}", column.cast_type.type_cast_from_user(value)) end define_method("#{name}_before_type_cast") do instance_variable_set("@#{name}_before_type_cast", column.default) unless instance_variable_defined?("@#{name}_before_type_cast") instance_variable_get("@#{name}_before_type_cast") end define_method(name) do instance_variable_set("@#{name}", column.default) unless instance_variable_defined?("@#{name}") instance_variable_get("@#{name}") end end end