diff --git a/app/models/concerns/atomic_base.rb b/app/models/concerns/atomic_base.rb index e279e7e..3fee9c7 100644 --- a/app/models/concerns/atomic_base.rb +++ b/app/models/concerns/atomic_base.rb @@ -12,12 +12,7 @@ module AtomicBase # atomic record extension class (contains the real data) -- ExtTopic def self.atomic_record_extension "Ext#{self}" - end - - # extension table -- :ext_topics - def self.extension_table - atomic_record_extension.constantize.table_name - end + end has_many :versions, ->{ order "id DESC"}, class_name: atomic_record_extension, foreign_key: :uuid @@ -25,13 +20,16 @@ def self.extension_table default_scope {includes(:data).where(deleted: false)} - end + # create forwarding accessors for the data columns + extension_class = atomic_record_extension.constantize + data_cols = extension_class.column_names - %w(id deleted uuid created_at update_at) + code = "" + data_cols.each do |col| + code << "def #{col}; data.#{col} end\n" + code << "def #{col}=(value); data.#{col} = value; end\n" + end + class_eval code - # delegate method calls to extension class - def method_missing(method, *args, &block) - self.data.send method, *args - rescue NoMethodError - super end # created returns created_at for the base diff --git a/app/models/concerns/atomic_extension.rb b/app/models/concerns/atomic_extension.rb index 1145a21..380d72a 100644 --- a/app/models/concerns/atomic_extension.rb +++ b/app/models/concerns/atomic_extension.rb @@ -9,8 +9,7 @@ module AtomicExtension # atomic record base class -- Topic def self.atomic_record_base - atomic_record_extension = self.to_s - atomic_record_extension.gsub(/\AExt/, '') + self.to_s.gsub(/\AExt/, '') end belongs_to :base, class_name: atomic_record_base, foreign_key: :uuid diff --git a/app/services/crud/delete.rb b/app/services/crud/delete.rb index cf828ce..20ebeb1 100644 --- a/app/services/crud/delete.rb +++ b/app/services/crud/delete.rb @@ -5,11 +5,5 @@ def initialize(base_record) super(base_record, {deleted: true}) end - def sql_update(ext_record) - "update #{base_record.class.table_name} " \ - "set ext_id = #{ext_record['id']}, deleted = true " \ - "where uuid = '#{base_record.uuid}'" - end - end end diff --git a/app/services/crud/update.rb b/app/services/crud/update.rb index 74fd485..4f9c330 100644 --- a/app/services/crud/update.rb +++ b/app/services/crud/update.rb @@ -9,21 +9,16 @@ def initialize(base_record, parameters) end def call - ActiveRecord::Base.transaction do - current_attributes = base_record.data.attributes.symbolize_keys - current_attributes.except!(:id, :created_at, :updated_at) - updated_attributes = parameters.reverse_merge(current_attributes) - ext_record = extension_resource_factory.create!(updated_attributes) - ActiveRecord::Base.connection.execute sql_update(ext_record) - base_record.reload + transaction_factory.transaction do + ext_params = base_record.data.attributes.symbolize_keys + ext_params.except!(:id, :deleted, :created_at, :updated_at) + ext_params.merge! parameters + new_ext = extension_resource_factory.create!(ext_params) + base_params = {ext_id: new_ext['id'], deleted: new_ext['deleted']} + base_record.update! base_params + base_record end end - def sql_update(ext_record) - "update #{base_record.class.table_name} " \ - "set ext_id = #{ext_record['id']} " \ - "where uuid = '#{base_record.uuid}'" - end - end end \ No newline at end of file diff --git a/spec/db_integration/topic_spec.rb b/spec/db_integration/topic_spec.rb index b953c89..9b7dea1 100644 --- a/spec/db_integration/topic_spec.rb +++ b/spec/db_integration/topic_spec.rb @@ -2,12 +2,13 @@ describe Topic do - it 'has the right extension class' do + it 'has the right extension class name' do expect(Topic.atomic_record_extension).to eq("ExtTopic") end it 'has the right extension table' do - expect(Topic.extension_table).to eq("ext_topics") + expect(Topic.atomic_record_extension.constantize.table_name).to \ + eq("ext_topics") end it 'returns the count of topics' do @@ -70,8 +71,8 @@ end it 'has the new title and description' do - expect(updated_topic.title).to eq("new title") - expect(updated_topic.description).to eq("new description") + expect(updated_topic.data.title).to eq("new title") + expect(updated_topic.data.description).to eq("new description") end it "reports 'created' as creation time of the original record" do