secretary-rails 1.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.md +186 -0
  3. data/Rakefile +9 -0
  4. data/app/models/secretary/version.rb +93 -0
  5. data/lib/generators/secretary/install_generator.rb +23 -0
  6. data/lib/generators/secretary/templates/versions_migration.rb +18 -0
  7. data/lib/secretary/config.rb +19 -0
  8. data/lib/secretary/engine.rb +4 -0
  9. data/lib/secretary/errors.rb +10 -0
  10. data/lib/secretary/gem_version.rb +3 -0
  11. data/lib/secretary/has_secretary.rb +75 -0
  12. data/lib/secretary/tracks_association.rb +146 -0
  13. data/lib/secretary/versioned_attributes.rb +58 -0
  14. data/lib/secretary-rails.rb +3 -0
  15. data/lib/secretary.rb +40 -0
  16. data/lib/tasks/secretary_tasks.rake +6 -0
  17. data/spec/factories.rb +32 -0
  18. data/spec/internal/app/models/animal.rb +7 -0
  19. data/spec/internal/app/models/hobby.rb +3 -0
  20. data/spec/internal/app/models/location.rb +6 -0
  21. data/spec/internal/app/models/person.rb +14 -0
  22. data/spec/internal/app/models/story.rb +3 -0
  23. data/spec/internal/app/models/user.rb +3 -0
  24. data/spec/internal/config/database.yml +3 -0
  25. data/spec/internal/config/routes.rb +2 -0
  26. data/spec/internal/db/combustion_test.sqlite +0 -0
  27. data/spec/internal/db/schema.rb +50 -0
  28. data/spec/internal/log/test.log +23177 -0
  29. data/spec/lib/generators/secretary/install_generator_spec.rb +17 -0
  30. data/spec/lib/secretary/config_spec.rb +9 -0
  31. data/spec/lib/secretary/has_secretary_spec.rb +116 -0
  32. data/spec/lib/secretary/tracks_association_spec.rb +214 -0
  33. data/spec/lib/secretary/versioned_attributes_spec.rb +63 -0
  34. data/spec/lib/secretary_spec.rb +44 -0
  35. data/spec/models/secretary/version_spec.rb +68 -0
  36. data/spec/spec_helper.rb +20 -0
  37. data/spec/tmp/db/migrate/20131105082639_create_versions.rb +18 -0
  38. metadata +181 -0
data/lib/secretary.rb ADDED
@@ -0,0 +1,40 @@
1
+ module Secretary
2
+ end
3
+
4
+ require 'secretary/engine'
5
+ require 'secretary/config'
6
+ require 'secretary/errors'
7
+ require 'diffy'
8
+
9
+ module Secretary
10
+ extend ActiveSupport::Autoload
11
+
12
+ class << self
13
+ # Pass a block to this method to define the configuration
14
+ # If no block is passed, config will be defaults
15
+ def configure
16
+ config = Config.new
17
+ yield config if block_given?
18
+ self.config = config
19
+ end
20
+
21
+ attr_writer :config
22
+ def config
23
+ @config || configure
24
+ end
25
+
26
+ def versioned_models
27
+ @versioned_models ||= []
28
+ end
29
+ end
30
+
31
+ autoload :HasSecretary
32
+ autoload :VersionedAttributes
33
+ autoload :TracksAssociation
34
+ end
35
+
36
+ ActiveSupport.on_load(:active_record) do
37
+ include Secretary::HasSecretary
38
+ include Secretary::TracksAssociation
39
+ include Secretary::VersionedAttributes
40
+ end
@@ -0,0 +1,6 @@
1
+ task :secretary do
2
+ task :setup do
3
+ # TODO Write code to generate a migration file and move the
4
+ # migration into it.
5
+ end
6
+ end
data/spec/factories.rb ADDED
@@ -0,0 +1,32 @@
1
+ FactoryGirl.define do
2
+ factory :animal do
3
+ name "Fred"
4
+ species "Elephant"
5
+ color "gray"
6
+ end
7
+
8
+ factory :location do
9
+ title "Crawford Family Forum"
10
+ address "474 S. Raymond, Pasadena"
11
+ end
12
+
13
+ factory :person do
14
+ name "Bryan"
15
+ ethnicity "none"
16
+ age 100
17
+ end
18
+
19
+ factory :story do
20
+ headline "Cool Headline"
21
+ body "Lorem, etc."
22
+ end
23
+
24
+ factory :user do
25
+ name "Bryan Ricker"
26
+ end
27
+
28
+ factory :version, class: "Secretary::Version" do
29
+ versioned { |v| v.association :story }
30
+ user
31
+ end
32
+ end
@@ -0,0 +1,7 @@
1
+ class Animal < ActiveRecord::Base
2
+ has_secretary
3
+
4
+ belongs_to :person
5
+
6
+ self.versioned_attributes = ["name", "color"]
7
+ end
@@ -0,0 +1,3 @@
1
+ class Hobby < ActiveRecord::Base
2
+ belongs_to :person
3
+ end
@@ -0,0 +1,6 @@
1
+ class Location < ActiveRecord::Base
2
+ has_secretary
3
+
4
+ has_many :people
5
+ tracks_association :people
6
+ end
@@ -0,0 +1,14 @@
1
+ class Person < ActiveRecord::Base
2
+ has_secretary
3
+
4
+ belongs_to :location
5
+
6
+ has_many :animals
7
+ accepts_nested_attributes_for :animals, allow_destroy: true
8
+
9
+ has_many :hobbies
10
+
11
+ tracks_association :animals, :hobbies
12
+
13
+ self.unversioned_attributes = ["name", "ethnicity"]
14
+ end
@@ -0,0 +1,3 @@
1
+ class Story < ActiveRecord::Base
2
+ has_secretary
3
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ has_many :activities, class_name: "Secretary::Version"
3
+ end
@@ -0,0 +1,3 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: db/combustion_test.sqlite
@@ -0,0 +1,2 @@
1
+ Rails.application.routes.draw do
2
+ end
@@ -0,0 +1,50 @@
1
+ ActiveRecord::Schema.define do
2
+ create_table "animals", force: true do |t|
3
+ t.string "name"
4
+ t.string "species"
5
+ t.string "color"
6
+ t.integer "person_id"
7
+ t.timestamps
8
+ end
9
+
10
+ create_table "hobbies", force: true do |t|
11
+ t.string "title"
12
+ t.integer "person_id"
13
+ t.timestamps
14
+ end
15
+
16
+ create_table "locations", force: true do |t|
17
+ t.string "title"
18
+ t.text "address"
19
+ t.timestamps
20
+ end
21
+
22
+ create_table "people", force: true do |t|
23
+ t.string "name"
24
+ t.string "ethnicity"
25
+ t.integer "age"
26
+ t.integer "location_id"
27
+ t.timestamps
28
+ end
29
+
30
+ create_table "stories", force: true do |t|
31
+ t.string "headline"
32
+ t.text "body"
33
+ t.timestamps
34
+ end
35
+
36
+ create_table "users", force: true do |t|
37
+ t.string "name"
38
+ t.timestamps
39
+ end
40
+
41
+ create_table "versions", force: true do |t|
42
+ t.integer "version_number"
43
+ t.string "versioned_type"
44
+ t.integer "versioned_id"
45
+ t.integer "user_id"
46
+ t.text "description"
47
+ t.text "object_changes"
48
+ t.datetime "created_at"
49
+ end
50
+ end