Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ changelog, see the [commits] for each version via the version links.

[commits]: https://github.com/scenic-views/scenic/commits/master

## [Unreleased]

Added

* Add a configuration value for changing the default path for the views files

[Unreleased]: https://github.com/scenic-views/scenic/compare/v1.5.2...master

## [1.5.2] - February 6, 2020

### Fixed
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,23 @@ def change
end
```

## Configuring Scenic

Currently there's two things that can be set up on this gem:

* `adapter` defines which adapter will be used to control the views on your database. By default we only provide and support for PostgreSQL, but you can set up other third-party adapters here

* `views_path` defines where the view SQL files will be saved. The defalt is on `db/views` on your Rails path.

To edit those values, create a `scenic.rb` file on your initialize directory and add the above code:

```ruby
Scenic.configure do |config|
config.database = MyDatabaseAdapter.new
config.views_path = 'my/path/to/view/files'
end
```

## FAQs

**Why do I get an error when querying a view-backed model with `find`, `last`, or `first`?**
Expand Down
8 changes: 8 additions & 0 deletions lib/scenic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@ def self.load
def self.database
configuration.database
end

# The current path for the views files.
#
# This defaults to 'db/views', but can be overridden
# via {Configuration}.
def self.views_path
configuration.views_path
end
end
13 changes: 13 additions & 0 deletions lib/scenic/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
module Scenic
class Configuration
# Set the default path to store the views.
#
# Defaults to db/views on Rails root
DEFAULT_VIEWS_PATH = "db/views".freeze

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

# The Scenic database adapter instance to use when executing SQL.
# The path where the views SQL files are stored
#
# Defaults to 'db/views'
# @return string
attr_accessor :views_path

def initialize
@database = Scenic::Adapters::Postgres.new
@views_path = DEFAULT_VIEWS_PATH
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/scenic/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def full_path
end

def path
File.join("db", "views", filename)
File.join(Scenic.views_path, filename)
end

def version
Expand Down
29 changes: 29 additions & 0 deletions spec/generators/scenic/view/view_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,33 @@
expect(migration).to contain(/create_view "non_public.searches"/)
end
end

context "for migrations using another view path" do
before do
Scenic.configure do |config|
config.views_path = "lib/dbms/views"
end
end

after { Scenic.configuration = Scenic::Configuration.new }

it "creates view files" do
view_definition = file("lib/dbms/views/searches_v01.sql")

run_generator ["search"]

expect(view_definition).to exist
end

it "updates an existing view" do
with_view_definition("searches", 1, "hello") do
view_definition = file("lib/dbms/views/searches_v02.sql")
allow(Dir).to receive(:entries).and_return(["searches_v01.sql"])

run_generator ["search"]

expect(view_definition).to exist
end
end
end
end
11 changes: 11 additions & 0 deletions spec/scenic/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ module Scenic
expect(Scenic.database).to eq adapter
end

it "allows the views path to be set" do
path = "db/pg/views"

Scenic.configure do |config|
config.views_path = path
end

expect(Scenic.configuration.views_path).to eq path
expect(Scenic.views_path).to eq path
end

def restore_default_config
Scenic.configuration = Configuration.new
end
Expand Down