Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Differentiated the names of the VersionMethods modules so that each calls included only once (Rails 4.1-compatibility) #107

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
31 changes: 31 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Tests
on: [push]

jobs:
ruby:
name: Rspec
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
ruby: [2.5, 2.6, 2.7]
activerecord: ["5.0", "5.1", "5.2", "6.0"]
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
- name: Set ActiveRecord Version
env:
AR_VERSION: ${{ matrix.activerecord }}
run: echo "gem 'activerecord', '~> $AR_VERSION'" >> Gemfile
- name: Install Sqlite
run: sudo apt-get install libsqlite3-dev
- name: Bundle
run: |
bundle config path vendor/bundle
bundle install --jobs 4 --retry 3
- name: Tests
run: bundle exec rake spec
22 changes: 0 additions & 22 deletions .travis.yml

This file was deleted.

3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ gemspec

group :test do
gem 'coveralls', '~> 0.7', :require => false
gem 'rspec', '~> 2.0'
gem 'rspec'
gem 'rspec-its'
gem 'sqlite3', '~> 1.0'
end

6 changes: 3 additions & 3 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ There are a handful of exciting new additions in version 1.0 of <tt>vestal_versi
@user.update_attribute(:first_name, "Stephen")
@user.first_name = "Steve"
@user.save
@user.update_attributes(:last_name => "Jobs")
@user.update(:last_name => "Jobs")
end
@user.version # => 1

Expand Down Expand Up @@ -154,12 +154,12 @@ There are a handful of exciting new additions in version 1.0 of <tt>vestal_versi

* Storing which user is responsible for a revision. Rather than introduce a lot of controller magic to guess what to store, you can simply update an additional attribute on your versioned model: <tt>updated_by</tt>.

@user.update_attributes(:last_name => "Jobs", :updated_by => "Tyler")
@user.update(:last_name => "Jobs", :updated_by => "Tyler")
@user.versions.last.user # => "Tyler"

Instead of passing a simple string to the <tt>updated_by</tt> setter, you can pass a model instance, such as an ActiveRecord user or administrator. The association will be saved polymorphically alongside the version.

@user.update_attributes(:last_name => "Jobs", :updated_by => current_user)
@user.update(:last_name => "Jobs", :updated_by => current_user)
@user.versions.last.user # => #<User first_name: "Steven", last_name: "Tyler">

* Global configuration. The new <tt>vestal_versions</tt> Rails generator also writes an initializer with instructions on how to set application-wide options for the <tt>versioned</tt> method.
Expand Down
10 changes: 0 additions & 10 deletions gemfiles/activerecord_3_0.gemfile

This file was deleted.

10 changes: 0 additions & 10 deletions gemfiles/activerecord_3_1.gemfile

This file was deleted.

10 changes: 0 additions & 10 deletions gemfiles/activerecord_3_2.gemfile

This file was deleted.

10 changes: 0 additions & 10 deletions gemfiles/activerecord_4_0.gemfile

This file was deleted.

4 changes: 2 additions & 2 deletions lib/vestal_versions/changes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def version_changes
# creation. Incremental changes are reset when the record is saved because they represent
# a subset of the dirty attribute changes, which are reset upon save.
def incremental_version_changes
changes.slice(*versioned_columns)
previous_changes.slice(*versioned_columns)
end

# Simply resets the cumulative changes after version creation.
Expand Down Expand Up @@ -118,4 +118,4 @@ def reverse_changes!
end
end
end
end
end
6 changes: 3 additions & 3 deletions lib/vestal_versions/control.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Control
# Control blocks are called on ActiveRecord::Base instances as to not cause any conflict with
# other instances of the versioned class whose behavior could be inadvertently altered within
# a control block.

# The +skip_version+ block simply allows for updates to be made to an instance of a versioned
# ActiveRecord model while ignoring all new version creation. The <tt>:if</tt> and
# <tt>:unless</tt> conditions (if given) will not be evaulated inside a +skip_version+ block.
Expand Down Expand Up @@ -55,7 +55,7 @@ def skip_version!
# user = User.find_by_first_name("Steve")
# user.version # => 1
# user.merge_version do
# user.update_attributes(:first_name => "Steven", :last_name => "Tyler")
# user.update(:first_name => "Steven", :last_name => "Tyler")
# user.update_attribute(:first_name, "Stephen")
# user.update_attribute(:last_name, "Richert")
# end
Expand Down Expand Up @@ -159,7 +159,7 @@ def create_version?
def update_version?
append_version?
end

module ClassMethods
# The +skip_version+ block simply allows for updates to be made to an instance of a versioned
# ActiveRecord model while ignoring all new version creation. The <tt>:if</tt> and
Expand Down
17 changes: 11 additions & 6 deletions lib/vestal_versions/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ module Users

included do
attr_accessor :updated_by
Version.class_eval{ include VersionMethods }
Version.class_eval{ include UserVersionMethods }
end

# Methods added to versioned ActiveRecord::Base instances to enable versioning with additional
# user information.


private
# Overrides the +version_attributes+ method to include user information passed into the
# parent object, by way of a +updated_by+ attr_accessor.
Expand All @@ -22,14 +22,19 @@ def version_attributes
end

# Instance methods added to VestalVersions::Version to accomodate incoming user information.
module VersionMethods
module UserVersionMethods
extend ActiveSupport::Concern

included do
belongs_to :user, :polymorphic => true

alias_method_chain :user, :name
alias_method_chain :user=, :name
# alias_method_chain :user, :name
alias_method :user_without_name, :user
alias_method :user, :user_with_name

# alias_method_chain :user=, :name
alias_method :user_without_name=, :user=
alias_method :user=, :user_with_name=
end

# Overrides the +user+ method created by the polymorphic +belongs_to+ user association. If
Expand Down
4 changes: 2 additions & 2 deletions lib/vestal_versions/version_tagging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def tag_version(tag)
end

# Instance methods included into VestalVersions::Version to enable version tagging.
module VersionMethods
module TaggingVersionMethods
extend ActiveSupport::Concern

included do
Expand All @@ -46,6 +46,6 @@ def validate_tags?
tagged? && tag != 'deleted'
end

Version.class_eval{ include VersionMethods }
Version.class_eval{ include TaggingVersionMethods }
end
end
2 changes: 1 addition & 1 deletion spec/support/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
:database => File.expand_path('../../test.db', __FILE__)
)

class CreateSchema < ActiveRecord::Migration
class CreateSchema < ActiveRecord::Migration[4.2]
def self.up
create_table :users, :force => true do |t|
t.string :first_name
Expand Down
4 changes: 2 additions & 2 deletions spec/vestal_versions/control_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@

user.append_version{ user.update_attribute(:last_name, 'Jobs') }

other_last_version = user.versions(true).last
other_last_version = user.versions.reload.last
other_last_version.id.should == original_id
other_last_version.attributes.should_not == original_attrs
end
Expand All @@ -110,7 +110,7 @@
user.update_attribute(:first_name, 'Steve')
end

other_last_version = user.versions(true).last
other_last_version = user.versions.reload.last
other_last_version.id.should == original_id
other_last_version.attributes.should_not == original_attrs
end
Expand Down
2 changes: 1 addition & 1 deletion spec/vestal_versions/deletion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

it "restores even if the schema has changed" do
new_mods = last_version.modifications.merge(:old_column => 'old')
last_version.update_attributes(:modifications => new_mods)
last_version.update(:modifications => new_mods)

last_version.restore.should == subject
end
Expand Down
2 changes: 1 addition & 1 deletion spec/vestal_versions/reset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
it 'dissociates all versions after the target' do
versions.reverse.each do |version|
subject.reset_to!(version)
subject.versions(true).after(version).count.should == 0
subject.versions.reload.after(version).count.should == 0
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/vestal_versions/reversion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@

it "does not store the reverted_from for subsequent saves" do
subject.revert_to!(1)
subject.update_attributes(:name => 'Bill Gates')
subject.update(:name => 'Bill Gates')
subject.versions.last.reverted_from.should be_nil
end

Expand All @@ -88,14 +88,14 @@
subject.revert_to(1)
subject.name = "Reverted"
subject.save
subject.update_attributes(:name => 'Bill Gates')
subject.update(:name => 'Bill Gates')
subject.versions.last.reverted_from.should be_nil
end

it "clears the reverted_from if the model is reloaded after a revert_to without a save" do
subject.revert_to(1)
subject.reload
subject.update_attributes(:name => 'Bill Gates')
subject.update(:name => 'Bill Gates')

subject.versions.last.reverted_from.should be_nil
end
Expand Down
6 changes: 3 additions & 3 deletions spec/vestal_versions/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
let(:user){ User.create(:name => 'Steve Richert') }

it 'defaults to nil' do
user.update_attributes(:first_name => 'Stephen')
user.update(:first_name => 'Stephen')
user.versions.last.user.should be_nil
end

it 'accepts and returns an ActiveRecord user' do
user.update_attributes(:first_name => 'Stephen', :updated_by => updated_by)
user.update(:first_name => 'Stephen', :updated_by => updated_by)
user.versions.last.user.should == updated_by
end

it 'accepts and returns a string user name' do
user.update_attributes(:first_name => 'Stephen', :updated_by => updated_by.name)
user.update(:first_name => 'Stephen', :updated_by => updated_by.name)
user.versions.last.user.should == updated_by.name
end
end
6 changes: 3 additions & 3 deletions spec/vestal_versions/version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
version.number.should == 1
version.should be_initial
end

it "sreturn the version number if it is not a revert" do
user.version.should == user.versions.last.original_number
end
Expand All @@ -53,9 +53,9 @@
it "return the original version if it is a double revert" do
user.revert_to!(2)
version = user.version
user.update_attributes(:last_name => 'Gates')
user.update(:last_name => 'Gates')
user.revert_to!(version)
user.versions.last.original_number.should == 2
end

end
8 changes: 4 additions & 4 deletions spec/vestal_versions/versions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@

it 'provides a version number for any given numeric version value' do
times.keys.each do |number|
subject.versions.number_at(number).should be_a(Fixnum)
subject.versions.number_at(number + 0.5).should be_a(Fixnum)
subject.versions.number_at(number).should be_a(Integer)
subject.versions.number_at(number + 0.5).should be_a(Integer)
subject.versions.number_at(number).should == subject.versions.number_at(number + 0.5)
end
end

it 'provides a version number for a valid tag' do
times.keys.map{|n| [n, n.to_s] }.each do |number, tag|
subject.versions.number_at(tag).should be_a(Fixnum)
subject.versions.number_at(tag).should be_a(Integer)
subject.versions.number_at(tag).should == number
end
end
Expand All @@ -155,7 +155,7 @@

it "provides a version number for any time after the model's creation" do
times.each do |number, time|
subject.versions.number_at(time + 30.minutes).should be_a(Fixnum)
subject.versions.number_at(time + 30.minutes).should be_a(Integer)
subject.versions.number_at(time + 30.minutes).should == number
end
end
Expand Down