Commit acdb1e0f by ivan Lan

Update json columns

parent e6ad01b5
module JsonColumns
def json_columns *columns
columns.each do |column|
define_method(column) {
value = self.read_attribute(column)
value && JSON.parse(value.gsub('=>', ':'))
}
define_method("#{column}=") { |value|
self.[]=( column, JSON.dump(value) )
}
end
end
end
\ No newline at end of file
......@@ -15,7 +15,7 @@ module WeatherModel
unique: nil,
belongs_to: nil,
has_many: nil,
json_columns: :list
json_column: :list
},
factory_name: :aqi_forecast,
factory: {
......
......@@ -23,9 +23,6 @@ module WeatherModel
options: {
index: %i[datetime name],
expire: 4 * 60 * 60,
unique: nil,
father: nil,
son: nil,
},
factory_name: :auto_station_ten_min,
factory: {
......
......@@ -20,7 +20,7 @@ module WeatherModel
unique: nil,
belongs_to: nil,
has_many: nil,
json_column: nil
},
factory_name: :city_forecast,
factory: {
......
......@@ -3,10 +3,8 @@ module WeatherModel
require 'active_record'
require 'ohm'
require "ohm/expire"
require 'weather-model/json_columns'
class Mysql < ::ActiveRecord::Base
extend JsonColumns
after_save :update_ohm
def self.use_settings settings
self.table_name = settings[:table_name]
......@@ -42,8 +40,21 @@ module WeatherModel
self.class.const_get('Ohm')
end
# Forecast -> :forecasts , haven't tested
def self.has_many name
super name.to_s.underscore.pluralize.to_sym
end
# Forecast -> :forecast
def self.belongs_to name
super name.to_s.underscore.to_sym
end
def self.json_column column
serialize column
end
class SourceOhm < ::Ohm::Model
extend JsonColumns
include ::Ohm::Expire
def self.use_settings settings
......@@ -56,6 +67,7 @@ module WeatherModel
settings[:schema].keys.each { |attr|
attribute attr
}
set_options settings
end
def self.set_options settings
......@@ -79,7 +91,6 @@ module WeatherModel
super
end
private
def self.has_many his_son # input like :Post
# collection :posts, :Post
......@@ -90,6 +101,14 @@ module WeatherModel
# reference :post, :Post
reference(his_dad.to_s.underscore.to_sym, his_dad ) if his_dad && SourceSchema.const_defined?(his_dad)
end
def self.json_column column
attribute column, ->(value) {
value.is_a?(String) ?
JSON.parse(value.gsub('=>', ':')) :
value
}
end
end
......@@ -100,17 +119,6 @@ module WeatherModel
ohm_obj = get_ohm.find(mysql_id: id).first.try(:update, ohm_attrs) || get_ohm.create(ohm_attrs)
# rescue Ohm::UniqueIndexViolation mysql_id should be unique
end
# Forecast -> :forecasts
def has_many name
super name.to_s.underscore.pluralize.to_sym
end
# Forecast -> :forecast
def belongs_to name
super name.to_s.underscore.to_sym
end
end
end
end
......@@ -62,7 +62,7 @@ RSpec.describe WeatherModel do
end
it 'aqi_forecast' do
@aqi_forecast = Aqi::Forecast.create(
@aqi_forecast = Aqi::AqiForecast.create(
datetime: Time.now,
prompt: '',
list: [
......@@ -79,7 +79,8 @@ RSpec.describe WeatherModel do
},
]
)
expect(Aqi::Forecast::Ohm.all.first).not_to be_nil, 'json_column 正常'
expect(Aqi::AqiForecast.all.first.list.class).to eq(Array), 'Mysql json_column 正常'
expect(Aqi::AqiForecast::Ohm.all.first.list.class).to eq(Array), 'Ohm json_column 正常'
end
after do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment