publishing_logic 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/.document +1 -1
  3. data/.gitignore +9 -23
  4. data/.rspec +2 -0
  5. data/.specification.example +34 -34
  6. data/.travis.yml +4 -0
  7. data/CODE_OF_CONDUCT.md +49 -0
  8. data/Gemfile +4 -0
  9. data/LICENSE +1 -1
  10. data/README.md +65 -0
  11. data/Rakefile +3 -49
  12. data/bin/console +14 -0
  13. data/bin/setup +8 -0
  14. data/lib/generators/publishing_logic/fields_generator.rb +13 -0
  15. data/lib/generators/publishing_logic/templates/migration.erb +10 -0
  16. data/lib/locale/en.yml +9 -0
  17. data/lib/locale/hu.yml +9 -0
  18. data/lib/publishing_logic.rb +7 -1
  19. data/lib/publishing_logic/model_logic.rb +51 -0
  20. data/lib/publishing_logic/version.rb +3 -0
  21. data/publishing_logic.gemspec +29 -0
  22. metadata +114 -176
  23. data/README.rdoc +0 -29
  24. data/VERSION +0 -1
  25. data/lib/model_logic.rb +0 -82
  26. data/rails_generators/publishing_logic_fields/USAGE +0 -2
  27. data/rails_generators/publishing_logic_fields/publishing_logic_fields_generator.rb +0 -34
  28. data/rails_generators/publishing_logic_fields/templates/app/views/publishing_logic_fields.html.erb +0 -12
  29. data/rails_generators/publishing_logic_fields/templates/db/migrate/add_publishing_logic_fields.rb.erb +0 -18
  30. data/spec/general_model_logic.rb +0 -93
  31. data/spec/models_with_all_fields_spec.rb +0 -85
  32. data/spec/models_with_no_published_until_field_spec.rb +0 -68
  33. data/spec/spec.opts +0 -1
  34. data/spec/spec_helper.rb +0 -55
  35. data/spec/support/database_cleanliness.rb +0 -14
  36. data/spec/support/database_connection.rb +0 -4
  37. data/spec/support/database_migrations.rb +0 -56
  38. data/spec/support/factories.rb +0 -10
  39. data/spec/support/logging.rb +0 -2
  40. data/spec/support/models/article.rb +0 -3
  41. data/spec/support/models/programme.rb +0 -3
@@ -1,2 +0,0 @@
1
- Real usage: ./script/generate publishing_logic_fields model_name [options]
2
- Note: The generator will fail if the model does not exist
@@ -1,34 +0,0 @@
1
- class PublishingLogicFieldsGenerator < Rails::Generator::NamedBase
2
- default_options :use_published_until_field => true, :skip_admin_form => false
3
-
4
- def initialize(args, options)
5
- super(args, options)
6
- end
7
-
8
- def manifest
9
- record do |m|
10
- class_name.camelize.constantize # Raise an error if model does not yet exist
11
- m.migration_template 'db/migrate/add_publishing_logic_fields.rb.erb', 'db/migrate', :assigns => {
12
- :migration_name => "AddPublishingLogicFieldsTo#{class_name.pluralize.gsub(/::/, '')}",
13
- :use_published_until_field => options[:use_published_until_field]
14
- }, :migration_file_name => "add_publishing_logic_fields_to_#{file_path.gsub(/\//, '_').pluralize}"
15
- unless options[:skip_admin_form]
16
- m.template 'app/views/publishing_logic_fields.html.erb',
17
- File.join('app', 'views', 'admin', plural_name, "_publishing_logic_fields.html.erb"),
18
- :assigns => {
19
- :use_published_until_field => options[:use_published_until_field]
20
- }
21
- end
22
- end
23
- end
24
-
25
- protected
26
- def add_options!(opt)
27
- opt.separator ''
28
- opt.separator 'Options:'
29
- opt.on("--no-published-until",
30
- "Don't add the published_until field to this model") { |v| options[:use_published_until_field] = false }
31
- opt.on("--skip-admin-form",
32
- "Don't generate the admin form partial for this model") { |v| options[:skip_admin_form] = true }
33
- end
34
- end
@@ -1,12 +0,0 @@
1
- <p>
2
- <%%= form.label :published_at %><br />
3
- <%%= form.datetime_select :published_at %>
4
- </p>
5
- <% if use_published_until_field %><p>
6
- <%%= form.label :published_until %><br />
7
- <%%= form.datetime_select :published_until %>
8
- </p><% end %>
9
- <p>
10
- <%%= form.label :publishing_enabled %>
11
- <%%= form.check_box :publishing_enabled %>
12
- </p>
@@ -1,18 +0,0 @@
1
- <% table_name = class_name.underscore.camelize.tableize %>
2
-
3
- class <%= migration_name %> < ActiveRecord::Migration
4
- def self.up
5
- change_table :<%= table_name %> do |t|
6
- t.boolean :publishing_enabled
7
- t.datetime :published_at<% if use_published_until_field %>, :published_until<% end %>
8
- t.index [:published_at, :publishing_enabled<% if use_published_until_field %>, :published_until<% end %>], :name => 'index_<%= table_name %>_on_publishing_logic_fields'
9
- end
10
- end
11
-
12
- def self.down
13
- change_table :<%= table_name %> do |t|
14
- t.remove :publishing_enabled, :published_at<% if use_published_until_field %>, :published_until<% end %>
15
- t.remove_index :name => 'index_<%= table_name %>_on_publishing_logic_fields'
16
- end
17
- end
18
- end
@@ -1,93 +0,0 @@
1
- shared_examples_for 'a model with publish logic' do
2
- def create_objects_with_different_published_at_dates(for_class)
3
- factory_name = for_class.name.underscore.to_sym
4
- @object2 = Factory.create(factory_name, :publishing_enabled => true, :published_at => 2.days.ago)
5
- @object1 = Factory.create(factory_name, :publishing_enabled => true, :published_at => 1.days.ago)
6
- @object3 = Factory.create(factory_name, :publishing_enabled => true, :published_at => 3.days.ago)
7
- end
8
-
9
- describe "ordering by published_at" do
10
- describe "from the published scope" do
11
- before do
12
- create_objects_with_different_published_at_dates(@class)
13
- end
14
-
15
- describe "newest first" do
16
- it "should be the most recently published object" do
17
- @class.published.newest.should == @object1
18
- end
19
- end
20
-
21
- describe "oldest first" do
22
- it "should be the object published the longest ago" do
23
- @class.published.oldest.should == @object3
24
- end
25
- end
26
- end
27
-
28
- describe "by publication date oldest first" do
29
- before do
30
- create_objects_with_different_published_at_dates(@class)
31
- end
32
-
33
- it "should return the items, oldest first" do
34
- @class.by_publication_date_oldest_first.map(&:id).should == [@object3,
35
- @object2,
36
- @object1].map(&:id)
37
- end
38
-
39
- it "should order by created_at date if published_ats are equal" do
40
- @object2b = Factory.create(@class.name.underscore.to_sym,
41
- :publishing_enabled => true,
42
- :published_at => @object2.published_at,
43
- :created_at => 3.days.ago)
44
- @class.by_publication_date_oldest_first.map(&:id).should == [@object3,
45
- @object2b,
46
- @object2,
47
- @object1].map(&:id)
48
- end
49
-
50
- it 'should have deprecated by_date_oldest_first' do
51
- assert_deprecated do
52
- @class.by_date_oldest_first
53
- end
54
- end
55
- end
56
-
57
- describe "by publication date newest first" do
58
- before do
59
- create_objects_with_different_published_at_dates(@class)
60
- end
61
-
62
- it "should return the items, newest first" do
63
- @class.by_publication_date_newest_first.map(&:id).should == [@object1,
64
- @object2,
65
- @object3].map(&:id)
66
- end
67
-
68
- it "should order by created_at date if published_ats are equal" do
69
- @object2b = Factory.create(@class.name.underscore.to_sym,
70
- :publishing_enabled => true,
71
- :published_at => @object2.published_at,
72
- :created_at => 3.days.from_now)
73
- @class.by_publication_date_newest_first.map(&:id).should == [@object1,
74
- @object2b,
75
- @object2,
76
- @object3].map(&:id)
77
- end
78
-
79
- it 'should have deprecated by_date_newest_first' do
80
- assert_deprecated do
81
- @class.by_date_newest_first
82
- end
83
- end
84
- end
85
-
86
- it "should have a newest first ordering that is the reverse of the oldest first ordering for identical objects" do
87
- creation_time = 2.days.ago
88
- publish_time = 1.days.ago
89
- 5.times { Factory.create(@class.name.underscore.to_sym, :created_at => creation_time, :published_at => publish_time) }
90
- @class.by_publication_date_newest_first.map(&:id).should == @class.by_publication_date_oldest_first.map(&:id).reverse
91
- end
92
- end
93
- end
@@ -1,85 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
2
- require 'general_model_logic'
3
-
4
- describe 'Using publishing logic on models with all fields' do
5
- describe "published?" do
6
- describe "with publishing enabled" do
7
- it "should be published by default" do
8
- Factory.create(:programme,
9
- :publishing_enabled => true,
10
- :published_at => nil,
11
- :published_until => nil).should be_published
12
- end
13
-
14
- it "should not be published if the published_at datetime is in the future" do
15
- Factory.create(:programme,
16
- :publishing_enabled => true,
17
- :published_at => 5.seconds.from_now,
18
- :published_until => nil).should_not be_published
19
- end
20
-
21
- it "should not be published if the published_until datetime is in the past" do
22
- Factory.create(:programme,
23
- :publishing_enabled => true,
24
- :published_at => nil,
25
- :published_until => 5.seconds.ago).should_not be_published
26
- end
27
- end
28
- describe "with publishing disabled" do
29
- it "should not be published" do
30
- Factory.create(:programme,
31
- :publishing_enabled => false,
32
- :published_at => 1.days.ago,
33
- :published_until => 10.days.from_now).should_not be_published
34
- end
35
- end
36
- end
37
-
38
- describe "published named scope" do
39
- it "should include published objects" do
40
- programme = Factory.create(:programme, :publishing_enabled => true)
41
- Programme.published.should == [programme]
42
- end
43
-
44
- it "should not include any unpublished objects" do
45
- Factory.create(:programme, :publishing_enabled => false)
46
- Programme.published.should be_empty
47
- end
48
-
49
- it "should not include objects with a published_until in the past" do
50
- Factory.create(:programme,
51
- :publishing_enabled => true,
52
- :published_until => 5.seconds.ago)
53
- Programme.published.should be_empty
54
- end
55
-
56
- it "should not include objects with a published_at in the future" do
57
- Factory.create(:programme,
58
- :publishing_enabled => true,
59
- :published_at => 5.seconds.from_now)
60
- Programme.published.should be_empty
61
- end
62
-
63
- it "should get a new Time.now for each invocation of the named scope" do
64
- mock_now = mock('now', :utc => 20.days.from_now, :to_f => 0)
65
- Time.stub(:now).and_return mock_now
66
- Programme.published
67
- end
68
-
69
- it "should use the utc of the current time" do
70
- # Make sure utc is used, which is hard to test as a behaviour
71
- mock_now = mock('now')
72
- Time.stub(:now).and_return mock_now
73
- mock_now.should_receive(:utc).twice
74
- Programme.published
75
- end
76
- end
77
-
78
- describe 'generally' do
79
- before do
80
- @class = Programme
81
- end
82
-
83
- it_should_behave_like 'a model with publish logic'
84
- end
85
- end
@@ -1,68 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
2
- require 'general_model_logic'
3
-
4
- describe 'Using publishing logic on models with no published until field' do
5
- describe "published?" do
6
- describe "with publishing enabled" do
7
- it "should be published by default" do
8
- Factory.create(:article,
9
- :publishing_enabled => true,
10
- :published_at => nil).should be_published
11
- end
12
-
13
- it "should not be published if the published_at datetime is in the future" do
14
- Factory.create(:article,
15
- :publishing_enabled => true,
16
- :published_at => 5.seconds.from_now).should_not be_published
17
- end
18
- end
19
- describe "with publishing disabled" do
20
- it "should not be published" do
21
- Factory.create(:article,
22
- :publishing_enabled => false,
23
- :published_at => 1.days.ago).should_not be_published
24
- end
25
- end
26
- end
27
-
28
- describe "published named scope" do
29
- it "should include published objects" do
30
- article = Factory.create(:article, :publishing_enabled => true)
31
- Article.published.should == [article]
32
- end
33
-
34
- it "should not include any unpublished objects" do
35
- Factory.create(:article, :publishing_enabled => false)
36
- Article.published.should be_empty
37
- end
38
-
39
- it "should not include objects with a published_at in the future" do
40
- Factory.create(:article,
41
- :publishing_enabled => true,
42
- :published_at => 5.seconds.from_now)
43
- Article.published.should be_empty
44
- end
45
-
46
- it "should get a new Time.now for each invocation of the named scope" do
47
- mock_now = mock('now', :utc => 20.days.from_now, :to_f => 0)
48
- Time.stub(:now).and_return mock_now
49
- Article.published
50
- end
51
-
52
- it "should use the utc of the current time" do
53
- # Make sure utc is used, which is hard to test as a behaviour
54
- mock_now = mock('now')
55
- Time.stub(:now).and_return mock_now
56
- mock_now.should_receive(:utc).once
57
- Article.published
58
- end
59
- end
60
-
61
- describe 'generally' do
62
- before do
63
- @class = Article
64
- end
65
-
66
- it_should_behave_like 'a model with publish logic'
67
- end
68
- end
data/spec/spec.opts DELETED
@@ -1 +0,0 @@
1
- --color
data/spec/spec_helper.rb DELETED
@@ -1,55 +0,0 @@
1
- require 'rubygems'
2
- gem 'rspec'
3
- gem 'rspec-rails'
4
- gem 'factory_girl'
5
- gem 'activesupport'
6
- gem 'activerecord'
7
- gem 'database_cleaner'
8
-
9
- require 'factory_girl'
10
- require 'active_support'
11
- require 'active_record'
12
-
13
- # We don't need all of spec/rails - just the bits that're to do with ActiveRecord
14
-
15
- require 'active_support/test_case'
16
- require 'active_record/fixtures' if defined?(ActiveRecord::Base)
17
-
18
- require 'spec/test/unit'
19
-
20
- require "spec/rails/example/model_example_group"
21
- require 'spec/rails/extensions/spec/matchers/have'
22
-
23
- require 'spec/rails/matchers/ar_be_valid'
24
- require 'spec/rails/matchers/change'
25
-
26
- require 'spec/rails/mocks'
27
-
28
- require 'spec/rails/extensions/active_support/test_case'
29
- require 'spec/rails/extensions/active_record/base'
30
-
31
- require 'spec/rails/interop/testcase'
32
-
33
- Spec::Example::ExampleGroupFactory.default(ActiveSupport::TestCase)
34
-
35
- require 'publishing_logic'
36
-
37
- require 'logger'
38
- ActiveRecord::Base.logger = Logger.new("test.log")
39
-
40
- # Time zone setup Publishing Logic assumes that you've properly set up your timezones.
41
- Time.zone_default = Time.send(:get_zone, 'UTC')
42
- ActiveRecord::Base.time_zone_aware_attributes = true
43
- ActiveRecord::Base.default_timezone = :utc
44
-
45
- Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f}
46
-
47
- # require 'publishing_logic'
48
- # require 'spec'
49
- # require 'spec/autorun'
50
-
51
- require 'database_cleaner'
52
- DatabaseCleaner.strategy = :truncation
53
-
54
- Spec::Runner.configure do |config|
55
- end
@@ -1,14 +0,0 @@
1
- Spec::Runner.configure do |config|
2
- config.before(:suite) do
3
- DatabaseCleaner.strategy = :transaction
4
- DatabaseCleaner.clean_with(:truncation)
5
- end
6
-
7
- config.before(:each) do
8
- DatabaseCleaner.start
9
- end
10
-
11
- config.after(:each) do
12
- DatabaseCleaner.clean
13
- end
14
- end
@@ -1,4 +0,0 @@
1
- ActiveRecord::Base.establish_connection(
2
- :adapter => "sqlite3",
3
- :database => "#{File.join(File.dirname(__FILE__),'test_publishing_logic.db')}"
4
- )
@@ -1,56 +0,0 @@
1
- module PublishingLogic
2
- module Migrations
3
- class AddProgrammes < ActiveRecord::Migration
4
- def self.up
5
- create_table :programmes do |t|
6
- t.boolean :publishing_enabled
7
- t.datetime :published_at
8
- t.datetime :published_until
9
- t.timestamps
10
- end
11
-
12
- add_index :programmes, [:published_at, :publishing_enabled, :published_until], :name => 'index_programmes_on_publishing_logic_fields'
13
- end
14
-
15
- def self.down
16
- remove_index :programmes, :name => 'index_programmes_on_publishing_logic_fields'
17
- end
18
- end
19
-
20
- class AddArticles < ActiveRecord::Migration
21
- def self.up
22
- create_table :articles do |t|
23
- t.boolean :publishing_enabled
24
- t.datetime :published_at
25
- t.timestamps
26
- end
27
-
28
- add_index :programmes, [:published_at, :publishing_enabled], :name => 'index_articles_on_publishing_logic_fields'
29
- end
30
-
31
- def self.down
32
- remove_index :articles, :name => 'index_articles_on_publishing_logic_fields'
33
- end
34
- end
35
- end
36
- end
37
-
38
- if ActiveRecord::Migrator.current_version != 2
39
- migrator = ActiveRecord::Migrator.new(:up, '', 2)
40
- migrator.instance_eval {
41
- migration_1 = ActiveRecord::MigrationProxy.new
42
- migration_1.instance_eval {
43
- @name = 'add_programmes'
44
- @version = 1
45
- @migration = PublishingLogic::Migrations::AddProgrammes
46
- }
47
- migration_2 = ActiveRecord::MigrationProxy.new
48
- migration_2.instance_eval {
49
- @name = 'add_articles'
50
- @version = 2
51
- @migration = PublishingLogic::Migrations::AddArticles
52
- }
53
- @migrations = [ migration_1, migration_2 ]
54
- }
55
- migrator.migrate
56
- end