Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

atomic record update without sql #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
22 changes: 10 additions & 12 deletions app/models/concerns/atomic_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,24 @@ 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

belongs_to :data, class_name: atomic_record_extension, foreign_key: :ext_id

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
Expand Down
3 changes: 1 addition & 2 deletions app/models/concerns/atomic_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions app/services/crud/delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 8 additions & 13 deletions app/services/crud/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 5 additions & 4 deletions spec/db_integration/topic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down