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 ...@@ -15,7 +15,7 @@ module WeatherModel
unique: nil, unique: nil,
belongs_to: nil, belongs_to: nil,
has_many: nil, has_many: nil,
json_columns: :list json_column: :list
}, },
factory_name: :aqi_forecast, factory_name: :aqi_forecast,
factory: { factory: {
......
...@@ -23,9 +23,6 @@ module WeatherModel ...@@ -23,9 +23,6 @@ module WeatherModel
options: { options: {
index: %i[datetime name], index: %i[datetime name],
expire: 4 * 60 * 60, expire: 4 * 60 * 60,
unique: nil,
father: nil,
son: nil,
}, },
factory_name: :auto_station_ten_min, factory_name: :auto_station_ten_min,
factory: { factory: {
......
...@@ -20,7 +20,7 @@ module WeatherModel ...@@ -20,7 +20,7 @@ module WeatherModel
unique: nil, unique: nil,
belongs_to: nil, belongs_to: nil,
has_many: nil, has_many: nil,
json_column: nil
}, },
factory_name: :city_forecast, factory_name: :city_forecast,
factory: { factory: {
......
...@@ -3,10 +3,8 @@ module WeatherModel ...@@ -3,10 +3,8 @@ module WeatherModel
require 'active_record' require 'active_record'
require 'ohm' require 'ohm'
require "ohm/expire" require "ohm/expire"
require 'weather-model/json_columns'
class Mysql < ::ActiveRecord::Base class Mysql < ::ActiveRecord::Base
extend JsonColumns
after_save :update_ohm after_save :update_ohm
def self.use_settings settings def self.use_settings settings
self.table_name = settings[:table_name] self.table_name = settings[:table_name]
...@@ -42,8 +40,21 @@ module WeatherModel ...@@ -42,8 +40,21 @@ module WeatherModel
self.class.const_get('Ohm') self.class.const_get('Ohm')
end 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 class SourceOhm < ::Ohm::Model
extend JsonColumns
include ::Ohm::Expire include ::Ohm::Expire
def self.use_settings settings def self.use_settings settings
...@@ -56,6 +67,7 @@ module WeatherModel ...@@ -56,6 +67,7 @@ module WeatherModel
settings[:schema].keys.each { |attr| settings[:schema].keys.each { |attr|
attribute attr attribute attr
} }
set_options settings
end end
def self.set_options settings def self.set_options settings
...@@ -79,7 +91,6 @@ module WeatherModel ...@@ -79,7 +91,6 @@ module WeatherModel
super super
end end
private private
def self.has_many his_son # input like :Post def self.has_many his_son # input like :Post
# collection :posts, :Post # collection :posts, :Post
...@@ -90,6 +101,14 @@ module WeatherModel ...@@ -90,6 +101,14 @@ module WeatherModel
# reference :post, :Post # reference :post, :Post
reference(his_dad.to_s.underscore.to_sym, his_dad ) if his_dad && SourceSchema.const_defined?(his_dad) reference(his_dad.to_s.underscore.to_sym, his_dad ) if his_dad && SourceSchema.const_defined?(his_dad)
end end
def self.json_column column
attribute column, ->(value) {
value.is_a?(String) ?
JSON.parse(value.gsub('=>', ':')) :
value
}
end
end end
...@@ -100,17 +119,6 @@ module WeatherModel ...@@ -100,17 +119,6 @@ module WeatherModel
ohm_obj = get_ohm.find(mysql_id: id).first.try(:update, ohm_attrs) || get_ohm.create(ohm_attrs) 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 # rescue Ohm::UniqueIndexViolation mysql_id should be unique
end end
# Forecast -> :forecasts end
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
end end
...@@ -62,7 +62,7 @@ RSpec.describe WeatherModel do ...@@ -62,7 +62,7 @@ RSpec.describe WeatherModel do
end end
it 'aqi_forecast' do it 'aqi_forecast' do
@aqi_forecast = Aqi::Forecast.create( @aqi_forecast = Aqi::AqiForecast.create(
datetime: Time.now, datetime: Time.now,
prompt: '', prompt: '',
list: [ list: [
...@@ -79,7 +79,8 @@ RSpec.describe WeatherModel do ...@@ -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 end
after do 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