From 0539e1b81ee709939643b26dfddf5073f81c7cce Mon Sep 17 00:00:00 2001 From: Simon Claessens Date: Mon, 25 Jan 2021 10:42:37 +0100 Subject: [PATCH] allow to configure definition path fix #261 --- lib/generators/scenic/view/view_generator.rb | 27 ++++----------- lib/scenic/configuration.rb | 12 ++++--- lib/scenic/definition.rb | 4 +-- lib/scenic/definitions.rb | 36 ++++++++++++++++++++ 4 files changed, 53 insertions(+), 26 deletions(-) create mode 100644 lib/scenic/definitions.rb diff --git a/lib/generators/scenic/view/view_generator.rb b/lib/generators/scenic/view/view_generator.rb index 496035f8..71b38d78 100644 --- a/lib/generators/scenic/view/view_generator.rb +++ b/lib/generators/scenic/view/view_generator.rb @@ -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 @@ -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 @@ -73,22 +70,12 @@ 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(?\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 @@ -96,7 +83,7 @@ def definition end def previous_definition - Scenic::Definition.new(plural_file_name, previous_version) + definitions.max || Scenic::Definition.new(plural_file_name, 0) end def destroying? diff --git a/lib/scenic/configuration.rb b/lib/scenic/configuration.rb index aec067fa..dc527c85 100644 --- a/lib/scenic/configuration.rb +++ b/lib/scenic/configuration.rb @@ -1,14 +1,18 @@ 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 diff --git a/lib/scenic/definition.rb b/lib/scenic/definition.rb index dac137cd..f1f109fb 100644 --- a/lib/scenic/definition.rb +++ b/lib/scenic/definition.rb @@ -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 diff --git a/lib/scenic/definitions.rb b/lib/scenic/definitions.rb new file mode 100644 index 00000000..2d59b11b --- /dev/null +++ b/lib/scenic/definitions.rb @@ -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 <=>(a, b) + a.version <=> 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(?\d+)\.sql\z/ + end + end +end