forked from ledermann/unread
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize .mark_as_read! and extend to work on relations
- Loading branch information
Showing
14 changed files
with
142 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
*.gem | ||
*.log | ||
.bundle | ||
Gemfile.lock | ||
gemfiles/*.lock | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Unread | ||
module Generators | ||
module GeneratorHelper | ||
def next_migration_number(dirname) | ||
if ActiveRecord::Base.timestamped_migrations | ||
Time.now.utc.strftime("%Y%m%d%H%M%S") | ||
else | ||
"%.3d" % (current_migration_number(dirname) + 1) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,17 @@ | ||
require 'rails/generators' | ||
require 'rails/generators/migration' | ||
require 'generators/unread/generator_helper' | ||
|
||
module Unread | ||
class MigrationGenerator < Rails::Generators::Base | ||
include Rails::Generators::Migration | ||
extend Unread::Generators::GeneratorHelper | ||
|
||
desc "Generates migration for read_markers" | ||
source_root File.expand_path('../templates', __FILE__) | ||
|
||
def create_migration_file | ||
migration_template 'migration.rb', 'db/migrate/unread_migration.rb' | ||
end | ||
|
||
def self.next_migration_number(dirname) | ||
if ActiveRecord::Base.timestamped_migrations | ||
Time.now.utc.strftime("%Y%m%d%H%M%S") | ||
else | ||
"%.3d" % (current_migration_number(dirname) + 1) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,31 @@ | ||
require 'generators/unread/migration_helper' | ||
|
||
class UnreadMigration < Unread::MIGRATION_BASE_CLASS | ||
extend Unread::Generators::MigrationHelper | ||
|
||
def self.up | ||
create_table ReadMark, force: true, options: create_options do |t| | ||
t.references :readable, polymorphic: { null: false } | ||
t.references :reader, polymorphic: { null: false } | ||
t.datetime :timestamp | ||
end | ||
|
||
add_index ReadMark, [:reader_id, :reader_type, :readable_type, :readable_id], name: 'read_marks_reader_readable_index', unique: true | ||
index_name = 'read_marks_reader_readable_index' | ||
add_index ReadMark, [:reader_id, :reader_type, :readable_type, :readable_id], name: index_name, unique: true | ||
|
||
if postgresql_9_5? | ||
execute <<-SQL | ||
ALTER TABLE #{ReadMark.table_name} | ||
ADD CONSTRAINT read_marks_reader_readable_constraint UNIQUE USING INDEX #{index_name} | ||
SQL | ||
end | ||
end | ||
|
||
def self.down | ||
drop_table ReadMark | ||
end | ||
|
||
def self.create_options | ||
options = '' | ||
if defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter) \ | ||
&& ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::Mysql2Adapter) | ||
options = 'DEFAULT CHARSET=latin1' | ||
end | ||
options | ||
mysql? ? 'DEFAULT CHARSET=latin1' : '' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Unread | ||
module Generators | ||
module MigrationHelper | ||
def mysql? | ||
defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter) \ | ||
&& ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::Mysql2Adapter) | ||
end | ||
|
||
def postgresql_9_5? | ||
defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) \ | ||
&& ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) \ | ||
&& ActiveRecord::Base.connection.send(:postgresql_version) >= 90500 | ||
end | ||
end | ||
end | ||
end |
10 changes: 2 additions & 8 deletions
10
lib/generators/unread/polymorphic_reader_migration/polymorphic_reader_migration_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,17 @@ | ||
require 'rails/generators' | ||
require 'rails/generators/migration' | ||
require 'generators/unread/generator_helper' | ||
|
||
module Unread | ||
class PolymorphicReaderMigrationGenerator < Rails::Generators::Base | ||
include Rails::Generators::Migration | ||
extend Unread::Generators::GeneratorHelper | ||
|
||
desc "Generates update migration to make reader of read_markers polymorphic" | ||
source_root File.expand_path('../templates', __FILE__) | ||
|
||
def create_migration_file | ||
migration_template 'unread_polymorphic_reader_migration.rb', 'db/migrate/unread_polymorphic_reader_migration.rb' | ||
end | ||
|
||
def self.next_migration_number(dirname) | ||
if ActiveRecord::Base.timestamped_migrations | ||
Time.now.utc.strftime("%Y%m%d%H%M%S") | ||
else | ||
"%.3d" % (current_migration_number(dirname) + 1) | ||
end | ||
end | ||
end | ||
end |
17 changes: 17 additions & 0 deletions
17
...ostgresql_unique_constraint_migration/postgresql_unique_constraint_migration_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require 'rails/generators' | ||
require 'rails/generators/migration' | ||
require 'generators/unread/generator_helper' | ||
|
||
module Unread | ||
class PostgresqlUniqueConstraintMigrationGenerator < Rails::Generators::Base | ||
include Rails::Generators::Migration | ||
extend Unread::Generators::GeneratorHelper | ||
|
||
desc "Generates update migration to add unique constraint for PostgresSQL database" | ||
source_root File.expand_path('../templates', __FILE__) | ||
|
||
def create_migration_file | ||
migration_template 'unread_postgresql_unique_constraint_migration.rb', 'db/migrate/unread_postgresql_unique_constraint_migration.rb' | ||
end | ||
end | ||
end |
31 changes: 31 additions & 0 deletions
31
...ql_unique_constraint_migration/templates/unread_postgresql_unique_constraint_migration.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require 'generators/unread/migration_helper' | ||
|
||
class UnreadPostgresqlUniqueConstraintMigration < Unread::MIGRATION_BASE_CLASS | ||
extend Unread::Generators::MigrationHelper | ||
|
||
INDEX_NAME = 'read_marks_reader_readable_index' | ||
INDEX_COLUMNS = [:reader_id, :reader_type, :readable_type, :readable_id] | ||
|
||
def self.up | ||
if postgresql_9_5? | ||
unless index_exists?(ReadMark, INDEX_COLUMNS, unique: true) | ||
add_index ReadMark, INDEX_COLUMNS, name: INDEX_NAME, unique: true | ||
end | ||
|
||
execute <<-SQL | ||
ALTER TABLE #{ReadMark.table_name} | ||
ADD CONSTRAINT read_marks_reader_readable_constraint UNIQUE USING INDEX #{INDEX_NAME} | ||
SQL | ||
end | ||
end | ||
|
||
def self.down | ||
if postgresql_9_5? | ||
execute <<-SQL | ||
ALTER TABLE #{ReadMark.table_name} | ||
DROP CONSTRAINT IF EXISTS read_marks_reader_readable_constraint | ||
SQL | ||
add_index ReadMark, INDEX_COLUMNS, name: INDEX_NAME, unique: true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module ActiveRecordRelation | ||
module Unread | ||
def mark_as_read!(options) | ||
klass.mark_as_read!(self, options) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters