Skip to content

Commit

Permalink
allow to configure definition path
Browse files Browse the repository at this point in the history
fix #261
  • Loading branch information
gagalago committed Jan 25, 2021
1 parent 3600a48 commit 10df05a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 26 deletions.
27 changes: 7 additions & 20 deletions lib/generators/scenic/view/view_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,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 +44,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 +70,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
14 changes: 10 additions & 4 deletions lib/scenic/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
require_relative "adapters/postgres"

module Scenic
class Configuration
include ActiveSupport::Configurable

# The Scenic database adapter instance to use when executing SQL.
#
# Defaults to an instance of {Adapters::Postgres}
# @return Scenic adapter
attr_accessor :database
config_accessor(:database) { Scenic::Adapters::Postgres.new }

def initialize
@database = Scenic::Adapters::Postgres.new
end
# The path where is stored views definition sql files.
#
# Defaults to an instance of {Pathname} to rails_root/db/views
# @return the path
config_accessor(:definitions_path) { Rails.root.join("db", "views") }
end

# @return [Scenic::Configuration] Scenic's current configuration
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)
Scenic.configuration.definitions_path.relative_path_from(Rails.root)
end

def version
Expand Down
36 changes: 36 additions & 0 deletions lib/scenic/definitions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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(plural_file_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

0 comments on commit 10df05a

Please sign in to comment.