sitepress-rails 4.0.9 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e5834bc1a71d0bcbf71d663532e8f08217cbb8281e8ba3afa0f0611229172a3b
4
- data.tar.gz: 7e12a1d5048024de09ca161cfb02940163e7eba950e3e2451f8adf03fdb4f359
3
+ metadata.gz: de65ed0cb60dae87a90fb914dd4dfd5183d4eccd6286e071d556533d1501e184
4
+ data.tar.gz: 4b0d4add465aea7037216b26dffbebf7ea000624740e8b5ab893acd97fa7cd65
5
5
  SHA512:
6
- metadata.gz: 198e16ce93c8c76d7d9b2a6452102a422905c9dca98bc45026aa5a0c370a5bfdc69f9d8c5cacfe3028dd195e4efbc4c8e337ec3066a9c650ae01f052ca03aacc
7
- data.tar.gz: b7588d454afb4d032594502ba2f946015edbd569c0772f9c427b1840a2b8d1fe7538a1eca4342332c8a22663c9eecc46ce8c66db1de0d6a18bbd79d002c2b3e5
6
+ metadata.gz: 3895c25f0bba45c456c28e6204650d8d15034609b1f8b5c14e7f86126aa9f7b12acab20c9baacc93f667aa406fae24bdfbbc60c90b30224a9f5f843f19cf5491
7
+ data.tar.gz: 5769f5831e3039e05835e5fd1a308df25621d3ac947b36d1ae3b39b13a3c7dcd892d594d745070d6fdb4246e406b92b5249777bbcea5f415660634a89e132f41
@@ -13,7 +13,7 @@ module Sitepress
13
13
  ]
14
14
 
15
15
  # Load the `config/site.rb` file so users can configure Sitepress.
16
- initializer :load_sitepress_file, before: :set_sitepress_paths do
16
+ initializer "sitepress.load_site_file", before: "sitepress.set_paths" do
17
17
  site_file = paths["config/site.rb"].existent.first
18
18
  load site_file if site_file
19
19
  end
@@ -24,49 +24,76 @@ module Sitepress
24
24
  "app/markdown" # When Sitepress is launched embedded in Rails project.
25
25
  ], autoload: true
26
26
 
27
- # Load paths from `Sitepress#site` into rails so it can render views, helpers, etc. properly.
28
- initializer :set_sitepress_paths, before: :set_autoload_paths do |app|
29
- app.paths["app/helpers"].push site.helpers_path.expand_path
27
+ # Load paths from `Sitepress#site` into Rails.
28
+ #
29
+ # We configure two separate systems:
30
+ #
31
+ # 1. app.paths["app/*"] - Rails component path registry
32
+ # Tells ActionView where to find templates, ActionController where to find helpers, etc.
33
+ #
34
+ # 2. config.autoload_paths - Zeitwerk autoloader configuration
35
+ # Tells Zeitwerk what to autoload. Rails automatically includes these in
36
+ # config.eager_load_paths for production environments.
37
+ #
38
+ initializer "sitepress.set_paths", before: :set_autoload_paths do |app|
39
+ site = Sitepress.configuration.site
40
+
41
+ # Helpers: autoloadable and available to controllers
42
+ # Collapsed so app/content/helpers/sample_helper.rb defines SampleHelper (not Helpers::SampleHelper)
43
+ site.helpers_path.expand_path.tap do |path|
44
+ app.paths["app/helpers"].push path
45
+ app.config.autoload_paths << path
46
+ app.config.eager_load_paths << path
47
+ Rails.autoloaders.main.collapse(path)
48
+ end
49
+
50
+ # Models: autoloadable
51
+ # Collapsed so models don't require namespace prefixes
52
+ site.models_path.expand_path.tap do |path|
53
+ app.paths["app/models"].push path
54
+ app.config.autoload_paths << path
55
+ app.config.eager_load_paths << path
56
+ Rails.autoloaders.main.collapse(path)
57
+ end
58
+
59
+ # Assets: available to Sprockets (no autoloading needed)
30
60
  app.paths["app/assets"].push site.assets_path.expand_path
61
+
62
+ # Views: available to ActionView (no autoloading needed - these are templates)
31
63
  app.paths["app/views"].push site.root_path.expand_path
32
64
  app.paths["app/views"].push site.pages_path.expand_path
33
- app.paths["app/models"].push site.models_path.expand_path
34
65
 
35
- # Set for view_components to load at ./components
66
+ # Components: autoloadable for view_components
36
67
  app.config.autoload_paths << File.expand_path("./components")
37
68
  end
38
69
 
39
70
  # Configure sprockets paths for the site.
40
- initializer :set_manifest_file_path, before: :append_assets_path do |app|
41
- manifest_file = sitepress_configuration.manifest_file_path.expand_path
71
+ initializer "sitepress.set_manifest_file_path", before: :append_assets_path do |app|
72
+ manifest_file = Sitepress.configuration.manifest_file_path.expand_path
42
73
  app.config.assets.precompile << manifest_file.to_s if manifest_file.exist?
43
74
  end
44
75
 
45
76
  # Configure Sitepress with Rails settings.
46
- initializer :configure_sitepress do |app|
47
- sitepress_configuration.parent_engine = app
77
+ initializer "sitepress.configure" do |app|
78
+ Sitepress.configuration.parent_engine = app
48
79
  # Reloads entire site between requests for development environments.
49
- sitepress_configuration.cache_resources = if app.config.respond_to? :enable_reloading?
80
+ Sitepress.configuration.cache_resources = if app.config.respond_to? :enable_reloading?
50
81
  # Rails 7.1 changed the name of this setting to enable_reloading, so check if that exist and use it.
51
82
  app.config.enable_reloading?
52
83
  else
53
84
  # Rails 7.0.x and lower all use this method to check if reloading is enabled.
54
85
  app.config.cache_classes
55
86
  end
56
-
57
- config.to_prepare do
58
- Sitepress::Path.handler_extensions = ActionView::Template::Handlers.extensions
59
- end
60
- end
61
-
62
- private
63
-
64
- def sitepress_configuration
65
- Sitepress.configuration
66
87
  end
67
88
 
68
- def site
69
- sitepress_configuration.site
89
+ # Set handler extensions once, after ActionView and all template engines are loaded.
90
+ initializer "sitepress.set_handler_extensions" do
91
+ ActiveSupport.on_load(:action_view) do
92
+ ActiveSupport.on_load(:after_initialize) do
93
+ Sitepress::Path.handler_extensions =
94
+ ActionView::Template::Handlers.extensions
95
+ end
96
+ end
70
97
  end
71
98
  end
72
99
  end
@@ -29,15 +29,23 @@ module Sitepress
29
29
  # Defines a class method that may be called later to return a
30
30
  # collection of objects. The default glob, for example, is named `:all`,
31
31
  # which defines `MyModel.all` on the class.
32
- def collection(name = Models::Collection::DEFAULT_NAME, glob:, **kwargs)
33
- define_singleton_method name do
34
- self.glob glob, **kwargs
32
+ def collection(name = :all, glob: nil, **, &)
33
+ if block_given?
34
+ build_collection(&)
35
+ else
36
+ ActiveSupport::Deprecation.new.warn(
37
+ "The `collection :#{name}, glob: ...` macro is deprecated. " \
38
+ "Use `def self.#{name} = glob('#{glob}')` instead."
39
+ )
40
+ define_singleton_method name do
41
+ self.glob glob, **
42
+ end
35
43
  end
36
44
  end
37
45
 
38
46
  # Adhoc querying of models via `Model.glob("foo/bar").all`
39
- def glob(glob, **kwargs)
40
- Models::Collection.new model: self, site: site, glob: glob, **kwargs
47
+ def glob(glob, **)
48
+ build_collection(model: self, **){ site.glob(glob) }
41
49
  end
42
50
 
43
51
  # Wraps a page in a class if given a string that represents the path or
@@ -67,6 +75,12 @@ module Sitepress
67
75
  def site
68
76
  Sitepress.site
69
77
  end
78
+
79
+ private
80
+
81
+ def build_collection(*, model: self, **, &)
82
+ Models::Collection.new(*, model:, **, &)
83
+ end
70
84
  end
71
85
  end
72
86
  end
@@ -5,28 +5,23 @@ module Sitepress
5
5
  class Collection
6
6
  include Enumerable
7
7
 
8
- # Page models will have `PageModel.all` method defined by default.
9
- DEFAULT_NAME = :all
8
+ attr_reader :model
10
9
 
11
- # Iterate over all resources in the site by default.
12
- DEFAULT_GLOB = "**/*.*".freeze
13
-
14
- attr_reader :model, :glob, :site
15
-
16
- def initialize(model:, site:, glob: DEFAULT_GLOB)
10
+ def initialize(model:, &resources)
17
11
  @model = model
18
- @glob = glob
19
- @site = site
12
+ @resources = resources
20
13
  end
21
14
 
22
15
  def resources
23
- site.glob glob
16
+ @resources.call
24
17
  end
25
18
 
26
19
  # Wraps each resource in a model object.
27
- def each
20
+ def each(&block)
21
+ return to_enum(:each) unless block_given?
22
+
28
23
  resources.each do |resource|
29
- yield model.new resource
24
+ yield model.new(resource)
30
25
  end
31
26
  end
32
27
  end
@@ -16,7 +16,6 @@ module Sitepress
16
16
 
17
17
  included do
18
18
  rescue_from Sitepress::ResourceNotFound, with: :resource_not_found
19
- helper Sitepress::Engine.helpers
20
19
  helper_method :current_page, :site
21
20
  around_action :ensure_site_reload
22
21
  end
@@ -125,10 +124,10 @@ module Sitepress
125
124
  # For whatever reason, Rails can't stablize this API so we need to check
126
125
  # the version of Rails to make the right call and stablize it.
127
126
  def find_layout(formats:)
128
- case Rails::VERSION::MAJOR
129
- when 8
127
+ case method(:_layout).arity
128
+ when 3
130
129
  _layout(lookup_context, formats, lookup_context.prefixes)
131
- else
130
+ when 2
132
131
  _layout(lookup_context, formats)
133
132
  end
134
133
  end
@@ -2,6 +2,8 @@ module Sitepress
2
2
  class InstallGenerator < Rails::Generators::Base
3
3
  source_root File.expand_path("templates", __dir__)
4
4
 
5
+ class_option :skip_markdown, type: :boolean, default: false, desc: "Skip adding markdown-rails gem"
6
+
5
7
  def copy_files
6
8
  directory ".", "app/content"
7
9
  end
@@ -12,9 +14,22 @@ module Sitepress
12
14
  end
13
15
 
14
16
  def append_sitepress_path_to_tailwind_config
17
+ # Tailwind v3
15
18
  if File.exist? 'config/tailwind.config.js'
16
- inject_into_file 'config/tailwind.config.js', ",\n './app/content/**/*.{erb,haml,html,slim,rb}'",
19
+ inject_into_file 'config/tailwind.config.js', ",\n './app/content/**/*.*'",
17
20
  after: " './app/views/**/*.{erb,haml,html,slim}'"
21
+ # Tailwind v4
22
+ elsif File.exist? 'app/assets/tailwind/application.css'
23
+ inject_into_file 'app/assets/tailwind/application.css', "\n@source \"../../content/**/*.*\";",
24
+ after: "@import \"tailwindcss\";"
25
+ end
26
+ end
27
+
28
+ def add_markdown_rails_gem
29
+ unless options[:skip_markdown]
30
+ gem "markdown-rails"
31
+ say "Added markdown-rails gem to Gemfile", :green
32
+ say "Run 'bundle install' to install the gem", :yellow
18
33
  end
19
34
  end
20
35
  end
@@ -45,7 +45,7 @@ title: Getting started with Sitepress in Rails
45
45
  <p>This was generated by the <code>rails generate sitepress:install</code> command.</p>
46
46
 
47
47
  <h2>Content</h2>
48
- <p>Sitepress content files are in <code><%= site.root_path.expand_path %></code>. There you can edit site content, including this file at <code><%= current_page.asset.path %></code>.
48
+ <p>Sitepress content files are in <code><%= site.root_path.expand_path %></code>. There you can edit site content, including this file at <code><%= current_page.asset.path %></code>.</p>
49
49
 
50
50
  <h2>Routes</h2>
51
51
  <p>Sitepress defined a root route in <code><%= Rails.root.join("config/routes.rb") %></code>. If that conflicts with your routes configuration, you'll want to open it and edit accordingly.</p>