drafteable 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.3"
4
+ # uncomment this line if your project needs to run something other than `rake`:
5
+ script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in drafteable.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Diego Fernandez Fernandez
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # Drafteable [![Build Status](https://travis-ci.org/littlemove/drafteable.png)](https://travis-ci.org/littlemove/drafteable)
2
+
3
+ Drafteable is a super simple and opinionated ActiveRecord model draft
4
+ behavior encapsulation:
5
+
6
+ A drafteable model should:
7
+
8
+ - Provide a boolean field as a flag for its draft status.
9
+ - Provide methods to check draft or published status.
10
+ - Provide methods to change draft or published status.
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ gem 'drafteable'
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install drafteable
25
+
26
+ ## Usage
27
+
28
+ On your ActiveRecord model add
29
+
30
+ acts_as_drafteable
31
+
32
+ Then you get `published?`, `draft?` (for checking the state) and `publish!`,
33
+ `draft!` (for changing the status).
34
+
35
+ Note that calling draft! will save the model skipping validations.
36
+
37
+ ## Contributing
38
+
39
+ 1. Fork it
40
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
41
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
42
+ 4. Push to the branch (`git push origin my-new-feature`)
43
+ 5. Create new Pull Request
44
+
45
+ You've got test under spec/ and `bundle install` should take care of
46
+ all gem dependencies for development.
47
+
48
+ ## TODO
49
+
50
+ 1. Write generator to create migration with draft field on model.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'drafteable/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "drafteable"
8
+ gem.version = Drafteable::VERSION
9
+ gem.authors = ["Diego Fernandez Fernandez"]
10
+ gem.email = ["diego.fernandez.fernandez@gmail.com"]
11
+ gem.description = %q{Drafteable is a super simple and opinionated ActiveRecord model draft
12
+ behavior encapsulation}
13
+ gem.summary = %q{Drafteable is a super simple and opinionated ActiveRecord model draft
14
+ behavior encapsulation}
15
+ gem.homepage = "http://github.com/littlemove/drafteable/"
16
+
17
+ gem.files = `git ls-files`.split($/)
18
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
19
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
20
+ gem.require_paths = ["lib"]
21
+
22
+ gem.add_dependency 'activerecord', '~> 3.2.8'
23
+ gem.add_development_dependency 'rspec', '~> 2.11.0'
24
+ gem.add_development_dependency 'sqlite3'
25
+ end
data/lib/drafteable.rb ADDED
@@ -0,0 +1,31 @@
1
+ require "drafteable/version"
2
+
3
+ module Drafteable
4
+ def acts_as_drafteable
5
+ include InstanceMethods
6
+ end
7
+
8
+ module InstanceMethods
9
+
10
+ def published?
11
+ begin
12
+ !self.draft
13
+ rescue NoMethodError
14
+ raise Exception.new('There is no draft field. Did you forget to run migrations?')
15
+ end
16
+ end
17
+
18
+ def draft!
19
+ self.draft = true
20
+ self.save(validate: false)
21
+ end
22
+
23
+ def publish!
24
+ self.draft = false
25
+ self.save
26
+ end
27
+
28
+ end
29
+
30
+ ActiveRecord::Base.extend Drafteable
31
+ end
@@ -0,0 +1,3 @@
1
+ module Drafteable
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,100 @@
1
+ require 'active_record'
2
+ require 'drafteable'
3
+
4
+ require 'fileutils'
5
+
6
+ describe Drafteable do
7
+
8
+ before :all do
9
+ setup_db
10
+ @no_draft_field_model = DummyModel.new
11
+ @drafteable_ready_model = DummyReadyModel.new
12
+ end
13
+
14
+ describe 'without draft field' do
15
+
16
+ it { @no_draft_field_model.should respond_to(:published?) }
17
+
18
+ it 'should raise Exception' do
19
+ lambda {
20
+ @no_draft_field_model.published?.should raise_exception(Exception, 'There is no draft field. Did you forget to run migrations?')
21
+ }
22
+ end
23
+
24
+ end
25
+
26
+
27
+ describe 'with draft field' do
28
+
29
+ it 'should not raise Exception' do
30
+ lambda { @drafteable_ready_model.published?.should_not raise_exception() }
31
+ end
32
+
33
+ it { @drafteable_ready_model.published?.should be(true) }
34
+
35
+ it { @drafteable_ready_model.draft?.should be(false) }
36
+
37
+ context 'set to draft' do
38
+ it 'should change to draft' do
39
+ @drafteable_ready_model.draft!
40
+ @drafteable_ready_model.draft?.should be(true)
41
+ end
42
+ end
43
+
44
+ context 'set to published' do
45
+ it 'should change to published' do
46
+ @drafteable_ready_model.publish!
47
+ @drafteable_ready_model.published?.should be(true)
48
+ end
49
+ end
50
+ end
51
+
52
+ after :all do
53
+ teardown_db
54
+ end
55
+
56
+ end
57
+
58
+ private
59
+
60
+ def setup_db
61
+ db_dir = 'spec/db'
62
+ FileUtils.mkdir(db_dir) unless File.exists?(db_dir)
63
+ ActiveRecord::Base.establish_connection(
64
+ adapter: 'sqlite3',
65
+ database: 'spec/db/development.sqlite3'
66
+ )
67
+
68
+ ActiveRecord::Migration.verbose = false
69
+
70
+ CreateDummyModels.migrate(:up)
71
+ end
72
+
73
+ def teardown_db
74
+ FileUtils.rm('spec/db/development.sqlite3')
75
+ end
76
+
77
+ class CreateDummyModels < ActiveRecord::Migration
78
+ def change
79
+ create_table :dummy_models do |t|
80
+ t.string :name
81
+
82
+ t.timestamps
83
+ end
84
+
85
+ create_table :dummy_ready_models do |t|
86
+ t.string :name
87
+ t.boolean :draft, default: false
88
+
89
+ t.timestamps
90
+ end
91
+ end
92
+ end
93
+
94
+ class DummyModel < ActiveRecord::Base
95
+ acts_as_drafteable
96
+ end
97
+
98
+ class DummyReadyModel < ActiveRecord::Base
99
+ acts_as_drafteable
100
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: drafteable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Diego Fernandez Fernandez
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.8
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.8
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 2.11.0
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.11.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: sqlite3
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: ! 'Drafteable is a super simple and opinionated ActiveRecord model draft
63
+
64
+ behavior encapsulation'
65
+ email:
66
+ - diego.fernandez.fernandez@gmail.com
67
+ executables: []
68
+ extensions: []
69
+ extra_rdoc_files: []
70
+ files:
71
+ - .gitignore
72
+ - .travis.yml
73
+ - Gemfile
74
+ - LICENSE.txt
75
+ - README.md
76
+ - Rakefile
77
+ - drafteable.gemspec
78
+ - lib/drafteable.rb
79
+ - lib/drafteable/version.rb
80
+ - spec/drafteable_spec.rb
81
+ homepage: http://github.com/littlemove/drafteable/
82
+ licenses: []
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 1.8.23
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: Drafteable is a super simple and opinionated ActiveRecord model draft behavior
105
+ encapsulation
106
+ test_files:
107
+ - spec/drafteable_spec.rb
108
+ has_rdoc: