recourse 0.1.0 → 1.0.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: d4eccbd88eefd636afb76f3e80b0f0f9de7d21edf9dfe98232304ed5e0617602
4
- data.tar.gz: 5b7ac4291838abc8e9935f276777400e9f1055a5c9a49a15804a722efc6c7248
3
+ metadata.gz: cd168efcdd88afa58c1b4bc5a5c7c4fc71bc91caec7b7a8ad344f41d7f2b3eb0
4
+ data.tar.gz: 4c6858fa894182eda4378571397b962fff778d172f7e749cc31e976776c71f50
5
5
  SHA512:
6
- metadata.gz: 494bad14557416cc18e4754c2510ff31e2468530766c4ea71fee59677a8402936e2921764c8da74e4b4252822cb68774e52e7a56adc5292840f645f18bfff9ef
7
- data.tar.gz: 97d241b130ca12c9db87490a6d41ea971df4478176628af4e3783bb64e7db658ea50c64255cdf25b6ed0cc1af74f59d105d3d69c5a46eddbdc85ea9d33aa52de
6
+ metadata.gz: 065fcb2ed2b597570045209d931b5dc74f6e12b30f39e3456dbbb7a635038a62d8b4e542764297df205b51c1163fe86937613ac5c6d69cd1def9f2dfeb40262d
7
+ data.tar.gz: ca93b998e63631993f14b55dc1ed8f2dcd3d9380eb57198f11e235cbaa366520a2f1247a4745e0c5c7b1cdcd9f1daf298b448ef975e29ac255d02ccca6e0340c
data/MIT-LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2026 Claudio Baccigalupo
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,39 +1,35 @@
1
- # Recourse
1
+ Recourse
2
+ ========
2
3
 
3
- TODO: Delete this and the text below, and describe your gem
4
+ Provides a new `recourses` method that can be invoked in a Rails app inside config/routes.rb
4
5
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/recourse`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+ `recourses` is like `resources` on steroids for admin-only routes:
6
7
 
7
- ## Installation
8
+ - All the routes are included in `Recourse.resources` to easily display in a navbar
9
+ - Their controllers do not need to define the `index` action: they inherit a predefined one
10
+ - There is also a predefined `index.html` view which displays the resources paginated/searchable.
11
+ - The content of each row can be customized defining a new `_row.html.erb` partial
8
12
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
13
+ How to install
14
+ ==============
10
15
 
11
- Install the gem and add to the application's Gemfile by executing:
16
+ 1. Add `gem 'resource'` to the `Gemfile` file of your Rails app.
12
17
 
13
- ```bash
14
- bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
- ```
18
+ Available methods
19
+ =================
16
20
 
17
- If bundler is not being used to manage dependencies, install the gem by executing:
21
+ In config/routes.rb:
18
22
 
19
- ```bash
20
- gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
21
- ```
23
+ - `recourses` inside the routes
22
24
 
23
- ## Usage
25
+ In a resourceful Active Record model:
24
26
 
25
- TODO: Write usage instructions here
27
+ - `recourse_includes`: the associations to include when fetching the resources
28
+ - `recourse_order`: the SQL to sort the resources by
29
+ - `recourse_positionable?`: whether the model has a position field to drag'n'drop sort
30
+ - `recourse_searchable?`: whether the model has Ransack enabled for searches
26
31
 
27
- ## Development
32
+ Anywhere:
28
33
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
34
+ - `Recourse.resources` to list all the routes
30
35
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
-
33
- ## Contributing
34
-
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/recourse.
36
-
37
- ## License
38
-
39
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile CHANGED
@@ -1,8 +1,6 @@
1
- # frozen_string_literal: true
1
+ require "bundler/setup"
2
2
 
3
- require "bundler/gem_tasks"
4
- require "rspec/core/rake_task"
5
-
6
- RSpec::Core::RakeTask.new(:spec)
3
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
7
5
 
8
- task default: :spec
6
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # Base class for controllers that require admin access
2
+ class RecourseController < ApplicationController
3
+ helper RecoursiveHelper, SearchableHelper
4
+ include Pagy::Method
5
+
6
+ def index
7
+ @model = controller_name.classify.constantize
8
+ @pagy, @resources = paginate @model, order: @model.recourse_order, includes: @model.recourse_includes
9
+ render 'recourse/index'
10
+ end
11
+
12
+ private
13
+
14
+ def paginate(model, includes: [], order: nil)
15
+ if model.recourse_searchable?
16
+ @q = model.ransack params[:q]
17
+ @q.sorts = (order || 'created_at desc') if @q.sorts.empty?
18
+ pagy includes.present? ? @q.result.includes(includes) : @q.result.distinct(true)
19
+ else
20
+ pagy @model.includes(includes).order(order)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ # A collection of helper methods used for navigation link in the admin-only section
2
+ module RecoursiveHelper
3
+ # Adds a link next to the title to add a new resource.
4
+ def button_link_to_add(name, path)
5
+ content_for(:edit) { button_link_to "Add #{name}", path, class: 'btn-sm btn-outline ms-3' }
6
+ end
7
+
8
+ private
9
+
10
+ def button_link_to(name, path, options = {})
11
+ link_to name, path, with_class('btn theme-primary', options)
12
+ end
13
+
14
+ def with_class(classes, options = {})
15
+ options.merge class: [options[:class], classes].compact.join(' ')
16
+ end
17
+ end
@@ -0,0 +1,50 @@
1
+ # A collection of helper methods used for navigation link in the admin-only section
2
+ module SearchableHelper
3
+ # Renders a form with a field to search within +model+ that uses +placeholder+.
4
+ # @param [String] q the search query.
5
+ # @param [ApplicationRecord] model the model of each object returned by the search
6
+ def search_form(q:, model:, url: url_for(model))
7
+ search_form_for(q, search_form_params(url: url)) do |form|
8
+ safe_join [filter_field_for(form, model:), search_field_for(form, model:)].compact
9
+ end
10
+ end
11
+
12
+ # Highlights the search query within the text of the matched content.
13
+ def search_highlight(content, model:)
14
+ highlight content.to_s, params.dig(:q, model.search_field)
15
+ end
16
+
17
+ private
18
+
19
+ def filter_field_for(form, model:)
20
+ method = model.filter_field
21
+ choices = model.filter_choices
22
+ options = { prompt: model.filter_prompt }
23
+ form.select method, choices, options, filter_field_params if method && choices
24
+ end
25
+
26
+ def search_field_for(form, model:)
27
+ form.search_field model.search_field, search_field_params(placeholder: model.search_placeholder)
28
+ end
29
+
30
+ def search_form_params(url:)
31
+ {
32
+ url: url, class: 'col-8 col-sm-6 col-md-4 d-flex', role: 'search',
33
+ data: { turbo_frame: :results, turbo_action: :advance },
34
+ }
35
+ end
36
+
37
+ def search_field_params(placeholder:)
38
+ {
39
+ class: 'form-control form-control-sm flex-grow-1', placeholder: placeholder,
40
+ oninput: 'debouncedSubmit(this.form)', aria: { label: 'Search' }
41
+ }
42
+ end
43
+
44
+ def filter_field_params
45
+ {
46
+ aria: { label: 'Filter' }, class: 'form-control form-control-sm me-3',
47
+ onchange: 'debouncedSubmit(this.form)',
48
+ }
49
+ end
50
+ end
@@ -0,0 +1,4 @@
1
+ <%# locals: (resource:, section:) -%>
2
+ <%= column section: section, header: 'ID' do %>
3
+ <%= resource.id %>
4
+ <% end %>
@@ -0,0 +1,21 @@
1
+ <% content_for :title, @model.name.pluralize %>
2
+
3
+ <% content_for :search do %>
4
+ <script>
5
+ function debounce(method) {
6
+ let timer
7
+ return (...args) => {
8
+ clearTimeout(timer)
9
+ timer = setTimeout(() => { method.apply(this, args) }, 300);
10
+ }
11
+ }
12
+ const debouncedSubmit = debounce((form) => form.requestSubmit())
13
+ </script>
14
+ <%= search_form q: @q, model: @model %>
15
+ <% end if @model.recourse_searchable? %>
16
+
17
+ <% button_link_to_add @model.name.underscore, url_for(action: :new) if @model.recourse_actions.include? :new %>
18
+
19
+ <%= table items: @resources, cached: @model.recourse_cachable?, positioned: @model.recourse_positionable?, pagy: @pagy, empty_label: "No #{@model.name.underscore.pluralize}" do |resource, section| %>
20
+ <%= render 'row', resource: resource, section: section %>
21
+ <% end %>
@@ -0,0 +1,10 @@
1
+ # A module to manage administered resources.
2
+ module Recourse
3
+ # Makes Recourse methods like `recourses` available in config/routes.rb
4
+ class Engine < ::Rails::Engine
5
+ config.to_prepare do
6
+ require 'recourse/routing'
7
+ require 'recourse/model'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,36 @@
1
+ # A module to manage administered resources.
2
+ module Recourse
3
+ # Ensures every Active Record object is compatible with recourse.
4
+ module Recoursive
5
+ # Return the associations to .include when loading all the resources.
6
+ def recourse_includes
7
+ %i[]
8
+ end
9
+
10
+ # Return the actions available for the model (among :new, :edit, :destroy)
11
+ def recourse_actions
12
+ default = %i[new edit destroy]
13
+ options = Recourse.resources[name.underscore.pluralize.to_sym]
14
+ (Array(options.fetch :only, default) & default) - Array(options.fetch :except, [])
15
+ end
16
+
17
+ # Return the fields to .order when loading all the models.
18
+ def recourse_order
19
+ end
20
+
21
+ # Return whether the content to display all the models can be cached.
22
+ def recourse_cachable?
23
+ true
24
+ end
25
+
26
+ # Return whether the model has a position field to use for drag'n'drop sorting.
27
+ def recourse_positionable?
28
+ false
29
+ end
30
+
31
+ # @return [Boolean] whether Ransack attributes are defined on the resource.
32
+ def recourse_searchable?
33
+ ransackable_attributes.any? || ransortable_attributes.any?
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,40 @@
1
+ # A module to manage administered resources.
2
+ module Recourse
3
+ # Extends any model to be searchable with Ransack.
4
+ module Searchable
5
+ # Returns the attributes that can be searched.
6
+ def ransackable_attributes(auth_object = nil)
7
+ %w[ ]
8
+ end
9
+
10
+ # Returns the associations that can be searched (by default: none).
11
+ def ransackable_associations(auth_object = nil)
12
+ %w[ ]
13
+ end
14
+
15
+ # Returns the associations that can be sorted by (by default: none).
16
+ def ransortable_attributes(auth_object = nil)
17
+ %w[ ]
18
+ end
19
+
20
+ # @note to be overriden by subclasses -- the name of the field to search with.
21
+ def search_field
22
+ end
23
+
24
+ # @note to be overriden by subclasses -- the placeholder for the field to search with.
25
+ def search_placeholder
26
+ end
27
+
28
+ # @note to be overriden by subclasses -- the name of the field to filter by.
29
+ def filter_field
30
+ end
31
+
32
+ # @note to be overriden by subclasses -- the prompt for the field to filter by.
33
+ def filter_prompt
34
+ end
35
+
36
+ # @note to be overriden by subclasses -- the values for the field to filter by.
37
+ def filter_choices
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,6 @@
1
+ require 'recourse/model/recoursive'
2
+ require 'recourse/model/searchable'
3
+
4
+ # TODO: Replace these with on_load
5
+ ActiveRecord::Base.extend Recourse::Recoursive
6
+ ActiveRecord::Base.extend Recourse::Searchable
@@ -0,0 +1,15 @@
1
+ # A module to manage administered resources.
2
+ module Recourse
3
+ # Makes Recourse methods like `recourses` available in config/routes.rb
4
+ module Routing
5
+ # This method is equivalent to Rails `resources` with the added bonus that we store
6
+ # the name of these administered resources so we can display them to admins in the navbar.
7
+ def recourses(*administered_resources, concerns: nil, **options, &block)
8
+ # NOTE: only is not enough, I need the except as well
9
+ administered_resources.each { |resource| Recourse.resources[resource] = options }
10
+ resources *administered_resources, concerns: concerns, **options, &block
11
+ end
12
+ end
13
+ end
14
+
15
+ ActionDispatch::Routing::Mapper.include Recourse::Routing
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module Recourse
4
- VERSION = "0.1.0"
2
+ VERSION = '1.0.0'
5
3
  end
data/lib/recourse.rb CHANGED
@@ -1,8 +1,15 @@
1
- # frozen_string_literal: true
1
+ require 'bh'
2
+ require 'pagy'
3
+ require 'ransack'
4
+ require 'recourse/engine'
2
5
 
3
- require_relative "recourse/version"
6
+ # @see https://ddnexus.github.io/pagy/resources/initializer/
7
+ Pagy::OPTIONS[:limit] = 15
8
+ # Pagy.options[:limit] = 15
4
9
 
10
+ # A module to manage administered resources.
5
11
  module Recourse
6
- class Error < StandardError; end
7
- # Your code goes here...
12
+ # Return all the administered resources.
13
+ mattr_reader :resources
14
+ @@resources = {}
8
15
  end
metadata CHANGED
@@ -1,14 +1,98 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: recourse
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
- - claudiob
8
- bindir: exe
7
+ - Claudio Baccigalupo
8
+ bindir: bin
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
- dependencies: []
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rails
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: bh
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: pagy
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ - !ruby/object:Gem::Dependency
55
+ name: ransack
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ type: :runtime
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ - !ruby/object:Gem::Dependency
69
+ name: minitest
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "<"
73
+ - !ruby/object:Gem::Version
74
+ version: '6'
75
+ type: :development
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "<"
80
+ - !ruby/object:Gem::Version
81
+ version: '6'
82
+ - !ruby/object:Gem::Dependency
83
+ name: simplecov
84
+ requirement: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ type: :development
90
+ prerelease: false
91
+ version_requirements: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
12
96
  description: Turns resources into recourses
13
97
  email:
14
98
  - claudiob@users.noreply.github.com
@@ -16,21 +100,28 @@ executables: []
16
100
  extensions: []
17
101
  extra_rdoc_files: []
18
102
  files:
19
- - ".rspec"
20
- - CHANGELOG.md
21
- - LICENSE.txt
103
+ - MIT-LICENSE
22
104
  - README.md
23
105
  - Rakefile
106
+ - app/controllers/recourse_controller.rb
107
+ - app/helpers/recoursive_helper.rb
108
+ - app/helpers/searchable_helper.rb
109
+ - app/views/recourse/_row.html.erb
110
+ - app/views/recourse/index.html.erb
24
111
  - lib/recourse.rb
112
+ - lib/recourse/engine.rb
113
+ - lib/recourse/model.rb
114
+ - lib/recourse/model/recoursive.rb
115
+ - lib/recourse/model/searchable.rb
116
+ - lib/recourse/routing.rb
25
117
  - lib/recourse/version.rb
26
- - sig/recourse.rbs
27
118
  homepage: https://github.com/claudiob/recourse
28
119
  licenses:
29
120
  - MIT
30
121
  metadata:
31
122
  homepage_uri: https://github.com/claudiob/recourse
32
- source_code_uri: https://github.com/claudiob/recourse
33
- changelog_uri: https://github.com/claudiob/recourse
123
+ source_code_uri: https://github.com/claudiob/recourse/
124
+ changelog_uri: https://github.com/claudiob/recourse/blob/master/CHANGELOG.md
34
125
  rdoc_options: []
35
126
  require_paths:
36
127
  - lib
@@ -38,14 +129,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
38
129
  requirements:
39
130
  - - ">="
40
131
  - !ruby/object:Gem::Version
41
- version: 3.1.0
132
+ version: '3'
42
133
  required_rubygems_version: !ruby/object:Gem::Requirement
43
134
  requirements:
44
135
  - - ">="
45
136
  - !ruby/object:Gem::Version
46
137
  version: '0'
47
138
  requirements: []
48
- rubygems_version: 3.6.9
139
+ rubygems_version: 4.0.3
49
140
  specification_version: 4
50
- summary: Rails admin tool.
141
+ summary: Rails admin tool
51
142
  test_files: []
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --color
3
- --require spec_helper
data/CHANGELOG.md DELETED
@@ -1,5 +0,0 @@
1
- ## [Unreleased]
2
-
3
- ## [0.1.0] - 2025-07-02
4
-
5
- - Initial release
data/LICENSE.txt DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2025 claudiob
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/recourse.rbs DELETED
@@ -1,4 +0,0 @@
1
- module Recourse
2
- VERSION: String
3
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
- end