Skip to content

Commit

Permalink
allow to configure definition path
Browse files Browse the repository at this point in the history
  • Loading branch information
gagalago committed Jan 25, 2021
1 parent 3600a48 commit a1944c6
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 24 deletions.
29 changes: 9 additions & 20 deletions lib/generators/scenic/view/view_generator.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require "rails/generators"
require "rails/generators/active_record"
require "generators/scenic/materializable"
require "scenic/definition"
require "scenic/definitions"

module Scenic
module Generators
Expand All @@ -11,8 +13,8 @@ class ViewGenerator < Rails::Generators::NamedBase
source_root File.expand_path("templates", __dir__)

def create_views_directory
unless views_directory_path.exist?
empty_directory(views_directory_path)
unless Scenic.configuration.definitions_path.exist?
empty_directory(Scenic.configuration.definitions_path)
end
end

Expand Down Expand Up @@ -44,10 +46,7 @@ def self.next_migration_number(dir)

no_tasks do
def previous_version
@previous_version ||=
Dir.entries(views_directory_path)
.map { |name| version_regex.match(name).try(:[], "version").to_i }
.max
@previous_version ||= previous_definition.version
end

def version
Expand All @@ -73,30 +72,20 @@ def activerecord_migration_class

private

alias singular_name file_name

def file_name
super.tr(".", "_")
end

def views_directory_path
@views_directory_path ||= Rails.root.join("db", "views")
end

def version_regex
/\A#{plural_file_name}_v(?<version>\d+)\.sql\z/
def definitions
@definitions ||= Scenic::Definitions.new(plural_file_name)
end

def creating_new_view?
previous_version.zero?
definitions.none?
end

def definition
Scenic::Definition.new(plural_file_name, version)
end

def previous_definition
Scenic::Definition.new(plural_file_name, previous_version)
definitions.max || Scenic::Definition.new(plural_file_name, 0)
end

def destroying?
Expand Down
1 change: 0 additions & 1 deletion lib/scenic.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require "scenic/configuration"
require "scenic/adapters/postgres"
require "scenic/command_recorder"
require "scenic/definition"
require "scenic/railtie"
require "scenic/schema_dumper"
require "scenic/statements"
Expand Down
11 changes: 10 additions & 1 deletion lib/scenic/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require_relative "adapters/postgres"

module Scenic
class Configuration
# The Scenic database adapter instance to use when executing SQL.
Expand All @@ -6,8 +8,15 @@ class Configuration
# @return Scenic adapter
attr_accessor :database

# The path where is stored views definition sql files.
#
# Defaults to an instance of {Pathname} to rails_root/db/views
# @return the path
attr_accessor :definitions_path

def initialize
@database = Scenic::Adapters::Postgres.new
self.database = Scenic::Adapters::Postgres.new
self.definitions_path = Rails.root
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/scenic/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ def to_sql
end

def full_path
Rails.root.join(path)
Scenic.configuration.definitions_path.join(filename)
end

def path
File.join("db", "views", filename)
full_path.relative_path_from(Rails.root).to_s
end

def version
Expand Down
38 changes: 38 additions & 0 deletions lib/scenic/definitions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require_relative "definition"

module Scenic
# @api private
class Definitions
include Enumerable
include Comparable

attr_reader :name, :views_directory_path

def initialize(name)
@name = name
end

def each
versions.each do |version|
yield Scenic::Definition.new(name, version)
end
end

def <=>(definition_a, definition_b)
definition_a.version <=> definition_b.version
end

def versions
@versions ||= Dir.entries(Scenic.configuration.definitions_path)
.map { |name| version_regex.match(name).try(:[], "version").try(:to_i) }
.compact
.sort
end

private

def version_regex
/\A#{name}_v(?<version>\d+)\.sql\z/
end
end
end
1 change: 1 addition & 0 deletions lib/scenic/statements.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require_relative "definition"
module Scenic
# Methods that are made available in migrations for managing Scenic views.
module Statements
Expand Down

0 comments on commit a1944c6

Please sign in to comment.