drafting 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 94ccad0d128a76e1c1aa944da416b5cee2ceb0be
4
+ data.tar.gz: 4611668c5f7f0fb09625d805fd2ad3bff2bb5abd
5
+ SHA512:
6
+ metadata.gz: 145c85f1ffcc5f7a9a1d24e219e88145c74a9a848337ad0d5a6349c8d12b7ded881538d21b44fbc7f1cacf2c62ae1c34984e6c7b35645b69bdc4b73d5efe7b9c
7
+ data.tar.gz: ad9209a1d0a2b713136854ae48bf809ce1df43b4d14a780d5862eb96f8b3ffd04598ede59d790235a53061463f6f9292dd75cf5ec29aa5ea648b18dc3ed04243
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in drafting.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Georg Ledermann
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # Drafting
2
+
3
+ This Ruby gem enhances `ActiveRecord::Base` to save a draft version of the current instance.
4
+
5
+ Remarkable:
6
+
7
+ * The gem stores all the data in **one** separate table and does not need to modify the existing tables
8
+ * It handles drafts for different models
9
+ * It allows saving draft for an object which does not pass the validations
10
+ * It uses marshaling, so associations and virtual attributes are saved, too
11
+ * A draft is optionally linked to a given user, so every user can manage his own drafts (invisible for the other users)
12
+ * A draft is optionally linked to a parent object. This helps showing existing drafts in a context (e.g. message drafts for a given topic)
13
+
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'drafting'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install drafting
30
+
31
+ ## Usage
32
+
33
+ TODO: Write usage instructions here
34
+
35
+ ## Development
36
+
37
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38
+
39
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ledermann/drafting. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](https://contributor-covenant.org) code of conduct.
44
+
45
+
46
+ ## License
47
+
48
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
49
+
50
+ Based on some code from the (probably) outdated gem [drafts](https://rubygems.org/gems/drafts) by Alexey Kuznetsov (@lxkuz)
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "drafting"
5
+
6
+ $LOAD_PATH.unshift(File.expand_path('..', File.dirname(__FILE__)))
7
+ require 'lib/generators/drafting/migration/templates/migration.rb'
8
+ require 'spec/support/spec_migration.rb'
9
+
10
+ require 'spec/models/user'
11
+ require 'spec/models/topic'
12
+ require 'spec/models/message'
13
+ require 'spec/models/page'
14
+ require 'spec/models/tag'
15
+
16
+ # You can add fixtures and/or initialization code here to make experimenting
17
+ # with your gem easier. You can also use a different console, if you like.
18
+
19
+ # (If you use this, don't forget to add pry to your Gemfile!)
20
+ # require "pry"
21
+ # Pry.start
22
+
23
+ ActiveRecord::Base.configurations = YAML.load_file('spec/database.yml')
24
+ ActiveRecord::Base.establish_connection(:sqlite)
25
+ ActiveRecord::Migration.verbose = false
26
+
27
+ DraftingMigration.up
28
+ SpecMigration.up
29
+
30
+ require "irb"
31
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/drafting.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'drafting/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "drafting"
8
+ spec.version = Drafting::VERSION
9
+ spec.authors = ["Georg Ledermann"]
10
+ spec.email = ["mail@georg-ledermann.de"]
11
+
12
+ spec.summary = %q{Save draft version of any ActiveRecord object}
13
+ spec.description = %q{}
14
+ spec.homepage = "https://github.com/ledermann/drafting"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "activerecord", ">= 3.0"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.10"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "sqlite3"
28
+ spec.add_development_dependency "factory_girl"
29
+ spec.add_development_dependency "coveralls"
30
+ end
data/lib/drafting.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'active_record'
2
+
3
+ require "drafting/version"
4
+ require "drafting/base_class_methods"
5
+ require "drafting/class_methods"
6
+ require "drafting/instance_methods"
7
+ require "drafting/draft"
8
+
9
+ ActiveRecord::Base.extend Drafting::BaseClassMethods
@@ -0,0 +1,29 @@
1
+ module Drafting
2
+ module BaseClassMethods
3
+ ALLOWED_DRAFT_OPTION_KEYS = [ :parent ]
4
+
5
+ def has_drafts(options={})
6
+ raise ArgumentError unless options.is_a?(Hash)
7
+ raise ArgumentError unless options.keys.all? { |k| ALLOWED_DRAFT_OPTION_KEYS.include?(k) }
8
+
9
+ class_attribute :draft_parent
10
+
11
+ if options[:parent]
12
+ parent_class = self.reflect_on_all_associations(:belongs_to).find { |a| a.name == options[:parent] }.try(:klass)
13
+ raise ArgumentError unless parent_class
14
+
15
+ parent_class.class_eval do
16
+ has_many :drafts, :as => :parent, :dependent => :nullify
17
+ end
18
+
19
+ self.draft_parent = options[:parent]
20
+ end
21
+
22
+ include Drafting::InstanceMethods
23
+ extend Drafting::ClassMethods
24
+
25
+ attr_accessor :draft_id
26
+ after_create :clear_draft
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,19 @@
1
+ module Drafting
2
+ module ClassMethods
3
+ def from_draft(draft_or_id)
4
+ draft = draft_or_id.is_a?(Draft) ? draft_or_id : Draft.find(draft_or_id)
5
+ raise ArgumentError unless draft.target_type == name
6
+
7
+ target = draft.target_type.constantize.new
8
+ target.load_from_draft(draft.data)
9
+
10
+ target.send("#{draft_parent}=", draft.parent) if draft_parent
11
+ target.draft_id = draft.id
12
+ target
13
+ end
14
+
15
+ def drafts(user)
16
+ Draft.where(:user => user, :target_type => name)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,10 @@
1
+ class Draft < ActiveRecord::Base
2
+ belongs_to :user
3
+ belongs_to :parent, :polymorphic => true
4
+
5
+ validates_presence_of :data, :target_type
6
+
7
+ def restore
8
+ target_type.constantize.from_draft(self)
9
+ end
10
+ end
@@ -0,0 +1,46 @@
1
+ module Drafting
2
+ module InstanceMethods
3
+ def save_draft(user=nil)
4
+ return false unless self.new_record?
5
+
6
+ draft = Draft.find_by_id(self.draft_id) || Draft.new
7
+
8
+ draft.data = dump_to_draft
9
+ draft.target_type = self.class.name
10
+ draft.user = user
11
+ draft.parent = self.send(self.class.draft_parent) if self.class.draft_parent
12
+
13
+ result = draft.save
14
+ self.draft_id = draft.id if result
15
+ result
16
+ end
17
+
18
+ def update_draft(user, attributes)
19
+ with_transaction_returning_status do
20
+ assign_attributes(attributes)
21
+ save_draft(user)
22
+ end
23
+ end
24
+
25
+ # Override this two methods if you want to change the way to dump/load data
26
+ def dump_to_draft
27
+ Marshal.dump(instance_values)
28
+ end
29
+
30
+ def load_from_draft(string)
31
+ values = Marshal.load(string)
32
+
33
+ values.each do |name, value|
34
+ instance_variable_set("@#{name}", value)
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def clear_draft
41
+ if draft = Draft.find_by_id(self.draft_id)
42
+ self.draft_id = nil if draft.destroy
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ module Drafting
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,23 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ module Drafting
5
+ class MigrationGenerator < Rails::Generators::Base
6
+ include Rails::Generators::Migration
7
+
8
+ desc 'Generates migration for Drafting'
9
+ source_root File.expand_path('../templates', __FILE__)
10
+
11
+ def create_migration_file
12
+ migration_template 'migration.rb', 'db/migrate/drafting_migration'
13
+ end
14
+
15
+ def self.next_migration_number(dirname)
16
+ if ActiveRecord::Base.timestamped_migrations
17
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
18
+ else
19
+ "%.3d" % (current_migration_number(dirname) + 1)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ class DraftingMigration < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :drafts do |t|
4
+ t.string :target_type, :null => false
5
+ t.references :user
6
+ t.references :parent, :polymorphic => true, :index => true
7
+ t.binary :data, :limit => 16777215, :null => false
8
+ t.datetime :updated_at, :null => false
9
+ end
10
+
11
+ add_index :drafts, [:user_id, :target_type]
12
+ end
13
+
14
+ def self.down
15
+ drop_table :drafts
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: drafting
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Georg Ledermann
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: factory_girl
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: coveralls
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: ''
112
+ email:
113
+ - mail@georg-ledermann.de
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - CODE_OF_CONDUCT.md
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - bin/console
127
+ - bin/setup
128
+ - drafting.gemspec
129
+ - lib/drafting.rb
130
+ - lib/drafting/base_class_methods.rb
131
+ - lib/drafting/class_methods.rb
132
+ - lib/drafting/draft.rb
133
+ - lib/drafting/instance_methods.rb
134
+ - lib/drafting/version.rb
135
+ - lib/generators/drafting/migration/migration_generator.rb
136
+ - lib/generators/drafting/migration/templates/migration.rb
137
+ homepage: https://github.com/ledermann/drafting
138
+ licenses:
139
+ - MIT
140
+ metadata: {}
141
+ post_install_message:
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubyforge_project:
157
+ rubygems_version: 2.4.8
158
+ signing_key:
159
+ specification_version: 4
160
+ summary: Save draft version of any ActiveRecord object
161
+ test_files: []