Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion arcdata_core.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ Gem::Specification.new do |s|
s.files = Dir["{app,config,db,lib}/**/*", "Rakefile", "README.rdoc"]
s.test_files = Dir["test/**/*"]

s.add_dependency "rails", "< 4.2"
s.add_dependency "rails", "~> 4.2"
s.add_dependency "activeadmin", "1.0.0.pre1"
end
43 changes: 24 additions & 19 deletions lib/core/serialized_columns.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
module Core
class SerializedColumn < ActiveRecord::ConnectionAdapters::Column
def type_cast(val)
case type
when :array then val.try(:split,',')
else super(val)
class ArrayType < ::ActiveRecord::Type::Value
def cast(value)
return value.try(:split, ',')
end
end

def type_cast_for_write(value)
case value
when Array then value.join(',')
else super(value)
def serialize(value)
return val.join(',')
end
end

def simplified_type(field_type)
case field_type
when 'array' then :array
else super(field_type)
end
CAST_TYPES = {
boolean: ::ActiveRecord::Type::Boolean,
integer: ::ActiveRecord::Type::Integer,
string: ::ActiveRecord::Type::String,
float: ::ActiveRecord::Type::Float,
date: ::ActiveRecord::Type::Date,
datetime: ::ActiveRecord::Type::DateTime,
decimal: ::ActiveRecord::Type::Decimal,
array: ArrayType,
any: ::ActiveRecord::Type::Value,
}

def initialize(name, default, cast_type, sql_type = nil, null = true)
super
@cast_type = CAST_TYPES.fetch(cast_type).new
end
end

Expand Down Expand Up @@ -54,29 +60,28 @@ def columns
end

def serialized_accessor store_attribute, name, type, default: nil
column = SerializedColumn.new name.to_s, default, type

sql_type = case type
sql_type = case column.type
when :string then 'varchar'
when :double then 'float'
when :time then 'timestamp'
else type.to_s
end

column = SerializedColumn.new name.to_s, default, sql_type

serialized_columns[name] = [store_attribute, column]

define_method name do
raw = read_store_attribute store_attribute, name
column.type_cast(raw)
column.type_cast_from_database(raw)
end

define_method :"#{name}_before_type_cast" do
read_store_attribute store_attribute, name
end

define_method :"#{name}=" do |val|
raw = column.type_cast_for_write(val)
raw = column.type_cast_for_database(val)
write_store_attribute store_attribute, name, raw
end

Expand Down