filepress 0.1.0 → 0.2.1

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: 1e18ee52d65d384ba64e31f69826907c5768e90530880e519b2e60b08c7b3792
4
- data.tar.gz: d0120941b4b5808438c7003c8a8fa9652587dd26eee6ee6c4f5d78efe2fd8336
3
+ metadata.gz: 7d19a795578233e2969fa1438a21c7614afab2650666a2a229338c2f322cccc0
4
+ data.tar.gz: cfcfbff322ea8909a384f580b6de6082f4a015808acb6d5e3f03a5bc02d17d54
5
5
  SHA512:
6
- metadata.gz: b375a0c1a6335d95df185db9c47d9b69d4366f7dd3176fb66ae0ff21fdd6ad8542019880bf68faf7f935249008991b0794570522eec79dc3c14da878f1f61770
7
- data.tar.gz: dd4b78d2e56ead2fc7b4c3c9233652c90840c1f1682b6eaf1b51e651e5e2b6ab4d505dfeadd8ba68a43555731c7fecdeb13ceb06b50873490f55b0cad1430b3a
6
+ metadata.gz: a69c6dbb2a9491da1698d9d97ba37ebb4e3e6caceffa1b0d4e7c21d782c0d5d4111869c075fcddb339dd0df471db291a9f43f197cd11dbb9871042b013b5f14c
7
+ data.tar.gz: 9590840f2f26567dca12b04fc929c2273a5f21074b179abdf7fb0f96dc22de5720a9d2bb37c8eede06c516ebb7bfcf7b1c8e0ae7eacc6192b782a5a3c9406a4e
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright Carl Dawson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,17 +1,122 @@
1
1
  # Filepress
2
2
 
3
- **Write content as flat files. Use it anywhere in your Rails app.**
3
+ Filepress is a minimal Rails plugin that offers the best of both worlds for content management:
4
+ - Write and version control your content in Markdown (or a similar format)
5
+ - Sync it seamlessly to ActiveRecord models so you can harness the full power of a Rails backend.
4
6
 
5
- Filepress lets you manage content in plain text files with frontmatter — like Markdown, HTML, or anything else and sync them directly into your ActiveRecord models. It's the simplicity of static files, with the full power of a Rails backend.
7
+ Ideal for blogs, documentation, or any content-driven Rails app where you want the flexibility of flat files and the structure of a database.
6
8
 
7
- ---
9
+ ## Getting Started
10
+
11
+ 1. Install the gem
12
+
13
+ Add it to your Gemfile:
14
+
15
+ ```ruby
16
+ gem "filepress"
17
+ ```
18
+ Then run:
19
+
20
+ ```bash
21
+ bundle install
22
+ ```
23
+
24
+ 2. Create a model to represent your content
25
+
26
+ Be sure to include a field that can serve as a unique identifier (e.g. `slug`):
27
+
28
+ ```bash
29
+ rails g model Post title:string slug:string body:text
30
+ ```
31
+
32
+ 3. Enable Filepress for your model
8
33
 
9
- ## Why use Filepress?
34
+ Use the `filepress` method in your model class:
10
35
 
11
- - 📝 Write content in any editor — no CMS required
12
- - 🔁 Keep content version-controlled with Git
13
- - ⚡ Instantly available in your Rails app via ActiveRecord
14
- - 🧠 Works with any file type (Markdown, HTML, plain text, etc.)
15
- - 🧹 Automatically adds, updates, and deletes records
36
+ ```ruby
37
+ class Post < ApplicationRecord
38
+ filepress
39
+ end
40
+ ```
16
41
 
42
+ 4. Add some content
43
+
44
+ Create Markdown files in `app/content/posts`. Each file should include frontmatter:
45
+
46
+ ```markdown
47
+ ---
48
+ title: My First Post
49
+ slug: first
17
50
  ---
51
+
52
+ # Hello, this is my first post
53
+ ```
54
+
55
+ 5. Sync your content
56
+
57
+ Run the sync task to import your files into the database:
58
+
59
+ ```bash
60
+ bin/rails filepress:sync
61
+ ```
62
+
63
+ That’s it! You’re now free to query your content via ActiveRecord like any other model. Filepress doesn't dictate how you render the body—use a Markdown parser like Kramdown, Redcarpet, or similar.
64
+
65
+ ## How it works
66
+
67
+ Filepress reads your content files, extracts YAML frontmatter, and uses the values to populate or update model attributes. The rest of the file becomes the value of the body attribute (or another field, if configured).
68
+
69
+ ## Configuration
70
+
71
+ You can customize how Filepress behaves by passing options to `filepress`:
72
+
73
+ ### `from:` - Set a custom content directory
74
+
75
+ ```ruby
76
+ class Post
77
+ filepress from: "app/my_custom_content_folder"
78
+ end
79
+ ```
80
+
81
+ ### `glob:` - Use filetypes besides Markdown
82
+
83
+ ```ruby
84
+ class Post
85
+ filepress glob: "*.html"
86
+ end
87
+ ```
88
+
89
+ ### `key:` - Use a unique identifier other than `slug`
90
+
91
+ ```ruby
92
+ class Post
93
+ filepress key: :name
94
+ end
95
+ ```
96
+
97
+ ### `body:` - Set which attribute stores the main content
98
+
99
+ ```ruby
100
+ class Post
101
+ filepress body: :content
102
+ end
103
+ ```
104
+
105
+ ### `destroy_stale:` - Prevent deletion of records when files are removed
106
+
107
+ By default, Filepress deletes records when the corresponding file is removed. Disable this behavior with:
108
+
109
+ ```ruby
110
+ class Post
111
+ filepress destroy_stale: false
112
+ end
113
+ ```
114
+
115
+ ## Why Filepress?
116
+
117
+ Filepress is for you if:
118
+
119
+ - You prefer writing content in files
120
+ - You want the convenience of version control for content
121
+ - And you still want powerful querying, associations, validations and all the other Rails goodness
122
+
data/Rakefile CHANGED
@@ -1,12 +1,2 @@
1
- # frozen_string_literal: true
2
-
1
+ require "bundler/setup"
3
2
  require "bundler/gem_tasks"
4
- require "minitest/test_task"
5
-
6
- Minitest::TestTask.create
7
-
8
- require "rubocop/rake_task"
9
-
10
- RuboCop::RakeTask.new
11
-
12
- task default: %i[test rubocop]
@@ -0,0 +1,15 @@
1
+ module Filepress
2
+ module Model
3
+ def filepress(from: nil, glob: "*.md", key: :slug, body: :body, destroy_stale: true)
4
+ class_attribute :filepress_options, instance_accessor: false, default: {}
5
+
6
+ self.filepress_options = {
7
+ from: from || "app/content/#{name.underscore.pluralize}",
8
+ glob: glob,
9
+ key: key,
10
+ body: body,
11
+ destroy_stale: destroy_stale
12
+ }
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ module Filepress
2
+ class Railtie < ::Rails::Railtie
3
+ ActiveSupport.on_load(:active_record) do
4
+ extend Filepress::Model
5
+ end
6
+
7
+ rake_tasks do
8
+ Dir[File.expand_path("../tasks/**/*.rake", __dir__)].each { |f| load f }
9
+ end
10
+ end
11
+ end
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module Filepress
4
- VERSION = "0.1.0"
2
+ VERSION = "0.2.1"
5
3
  end
data/lib/filepress.rb CHANGED
@@ -1,77 +1,40 @@
1
- # frozen_string_literal: true
2
-
3
- require "active_support"
4
- require "active_support/core_ext"
5
- require "active_support/concern"
6
-
7
- require_relative "filepress/version"
1
+ require "filepress/version"
2
+ require "filepress/railtie"
3
+ require "filepress/model"
8
4
 
9
5
  module Filepress
10
- class Syncer
11
- def self.sync!(model_class)
12
- opts = model_class.filepress_options
13
- raise "Missing Filepress Options" unless opts
6
+ class << self
7
+ def sync
8
+ Rails.application.eager_load!
9
+ models = ActiveRecord::Base.descendants.select { |model| model.respond_to? :filepress_options }
14
10
 
15
- path = Rails.root.join(opts[:path])
16
- key = opts[:key]
17
- content_attr = opts[:content_attribute]
11
+ models.each do |model|
12
+ options = model.filepress_options
13
+ raise "Filepress options missing for #{model.name}" unless options
18
14
 
19
- seen_keys = []
15
+ path = Rails.root.join(options[:from])
16
+ key = options[:key]
17
+ body_attribute = options[:body]
20
18
 
21
- Dir.glob("#{path}/#{opts[:glob]}").each do |file|
22
- raw = File.read(file)
23
- frontmatter, body = raw.split(/^---\s*$/, 3).reject(&:empty?)
24
- data = YAML.safe_load(frontmatter, symbolize_names: true)
25
- content = body.strip
19
+ keys = Dir.glob("#{path}/#{options[:glob]}").map do |file|
20
+ raw = File.read(file)
21
+ frontmatter, body = raw.split(/^---\s*$/, 3).reject(&:empty?)
22
+ data = YAML.safe_load(frontmatter, symbolize_names: true)
23
+ content = body.strip
26
24
 
27
- identifier = data[key]
28
- raise "Missing key `#{key}` in frontmatter for #{file}" unless identifier
25
+ identifier = data[key]
26
+ raise "Missing key `#{key}` in frontmatter for #{file}" unless identifier
29
27
 
30
- seen_keys << identifier
28
+ record = model.find_or_initialize_by(key => identifier)
29
+ record.assign_attributes(data)
30
+ record.send("#{body_attribute}=", content)
31
+ record.save!
31
32
 
32
- record = model_class.find_or_initialize_by(key => identifier)
33
- record.assign_attributes(data)
34
- record.send("#{content_attr}=", content)
35
- record.save!
36
- end
33
+ identifier
34
+ end
37
35
 
38
- # Clean up records whose source file no longer exists
39
- model_class.where.not(key => seen_keys).destroy_all
40
- end
41
-
42
- def self.sync_all!
43
- models = ActiveRecord::Base.descendants.select do |klass|
44
- klass.respond_to?(:filepress_options)
36
+ model.where.not(key => keys).destroy_all if options[:destroy_stale]
45
37
  end
46
-
47
- models.each { |model| sync!(model) }
48
- end
49
- end
50
-
51
- module ModelExt
52
- def filepress_model(path: nil, glob: "*.md", key: :slug, content_attribute: :content)
53
- class_attribute :filepress_options, instance_accessor: false, default: {}
54
-
55
- self.filepress_options = {
56
- path: path || "app/content/#{name.underscore.pluralize}",
57
- glob: glob,
58
- key: key,
59
- content_attribute: content_attribute
60
- }
61
- end
62
- end
63
- end
64
-
65
- ActiveSupport.on_load(:active_record) do
66
- extend Filepress::ModelExt
67
- end
68
-
69
- if defined?(Rake::Task)
70
- namespace :filepress do
71
- desc "Sync all filepress-backed models"
72
- task sync: :environment do
73
- Rails.application.eager_load!
74
- Filepress::Syncer.sync_all!
75
38
  end
76
39
  end
77
40
  end
@@ -0,0 +1,6 @@
1
+ namespace :filepress do
2
+ desc "Synchronise Filepress models"
3
+ task sync: :environment do
4
+ Filepress.sync
5
+ end
6
+ end
metadata CHANGED
@@ -1,11 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filepress
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carl Dawson
8
- bindir: exe
8
+ bindir: bin
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
@@ -31,20 +31,21 @@ executables: []
31
31
  extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
- - ".rubocop.yml"
35
- - LICENSE.txt
34
+ - MIT-LICENSE
36
35
  - README.md
37
36
  - Rakefile
38
37
  - lib/filepress.rb
38
+ - lib/filepress/model.rb
39
+ - lib/filepress/railtie.rb
39
40
  - lib/filepress/version.rb
40
- - sig/filepress.rbs
41
+ - lib/tasks/filepress_tasks.rake
41
42
  homepage: https://github.com/carldaws/filepress
42
43
  licenses:
43
44
  - MIT
44
45
  metadata:
45
46
  allowed_push_host: https://rubygems.org
46
47
  homepage_uri: https://github.com/carldaws/filepress
47
- source_code_uri: https://github.com/carldawson/filepress
48
+ source_code_uri: https://github.com/carldaws/filepress
48
49
  rdoc_options: []
49
50
  require_paths:
50
51
  - lib
@@ -52,7 +53,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
52
53
  requirements:
53
54
  - - ">="
54
55
  - !ruby/object:Gem::Version
55
- version: 3.1.0
56
+ version: '0'
56
57
  required_rubygems_version: !ruby/object:Gem::Requirement
57
58
  requirements:
58
59
  - - ">="
data/.rubocop.yml DELETED
@@ -1,8 +0,0 @@
1
- AllCops:
2
- TargetRubyVersion: 3.1
3
-
4
- Style/StringLiterals:
5
- EnforcedStyle: double_quotes
6
-
7
- Style/StringLiteralsInInterpolation:
8
- EnforcedStyle: double_quotes
data/LICENSE.txt DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2025 Carl Dawson
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
data/sig/filepress.rbs DELETED
@@ -1,4 +0,0 @@
1
- module Filepress
2
- VERSION: String
3
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
- end