Skip to content

Commit

Permalink
update for namespaces. etc
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffpeterson committed Dec 2, 2024
1 parent 72d288f commit 5be25af
Show file tree
Hide file tree
Showing 16 changed files with 54 additions and 19 deletions.
8 changes: 7 additions & 1 deletion app/presenters/cafe_car/active_record/base_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ def associations(*names, **options, &block)
end

def to_model = object
def to_html = link_to title, [self] rescue title
def to_html
if partial? object.to_partial_path
render object
else
link_to title, [self] rescue title
end
end
end
end
end
3 changes: 2 additions & 1 deletion app/presenters/cafe_car/presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module CafeCar
class Presenter
attr_reader :object, :options

delegate *%w[l t capture concat link_to render safe_join tag ui], to: :@template
delegate *%w[l t capture concat link_to partial? render safe_join tag ui], to: :@template

def self.present(template, object, **options)
object = object.object if object.is_a?(Presenter)
Expand Down Expand Up @@ -48,6 +48,7 @@ def attributes(*methods, except: nil, **options, &block)
end

def attribute(method, **options, &block)
# TODO: rescue from missing attribute errors and suggest checking the policy
@shown_attributes[method] = true
content = show(method, **options, &block).to_s
return "" if content.blank?
Expand Down
4 changes: 4 additions & 0 deletions app/views/application/_filters.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
= ui.card do |card|
= filter_form_for objects do |f|

= card.section do
- if title = policy(objects).title_attribute
= f.field "#{title}~", label: "Search"

- f.remaining_attributes.each do |attr|
= f.field attr

Expand Down
2 changes: 1 addition & 1 deletion app/views/application/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
= form_for object do |f|
= form_for [*namespace, object] do |f|
= card.section do
= render "fields", f:

Expand Down
2 changes: 1 addition & 1 deletion lib/cafe_car/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def object=(value)
def model_name = model.model_name

def model
@model ||= self.class.name.gsub(/.*::|Controller$/, '').singularize.constantize
@model ||= self.class.name.gsub(/.*::|Controller$/, '').classify.then { self.class.module_parent.const_get _1 }
end

def created_redirect = redirect_back fallback_location: [model_name.plural.to_sym]
Expand Down
8 changes: 6 additions & 2 deletions lib/cafe_car/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ def present(*args, **options)

def current_href?(...) = current_page? href_for(...)

def href_for(*parts, **params)
def href_for(*parts, namespace: self.namespace, **params)
params.merge! parts.extract_options!
params.delete(:action) if %i[show destroy index].include? params[:action]
url_for([*parts, params])
url_for([*namespace, *parts, params])
end

def link(object)
Expand Down Expand Up @@ -78,5 +78,9 @@ def get_partial(path)
prefixes = path.include?(?/) ? [] : lookup_context.prefixes
lookup_context.find(path, prefixes, true)
end

def namespace
@namespace ||= controller_path.split("/").tap(&:pop).map(&:to_sym)
end
end
end
13 changes: 7 additions & 6 deletions lib/cafe_car/link_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ class LinkBuilder

delegate :link_to, :link_to_unless, :current_page?, :href_for, to: :@template

def initialize(template, object)
@template = template
@object = object
def initialize(template, object, namespace: template.namespace)
@template = template
@object = object
@namespace = namespace
end

def model = @object.is_a?(Class) ? @object : @object.class
Expand All @@ -28,8 +29,8 @@ def disabled(action, reason) = i18n(action, scope: [:disabled, reason])

def turbo!(opts)
opts.replace({
data: {turbo_stream: true,
turbo_method: opts.delete(:method),
data: {turbo_stream: true,
turbo_method: opts.delete(:method),
turbo_confirm: opts.delete(:confirm)
}
}.deep_merge(opts))
Expand All @@ -39,7 +40,7 @@ def link(action, target, label = i18n(action), disabled: false, hide: false, **o
disabled ||= cant?(action)
return "" if disabled and hide

href = href_for(*target, action:)
href = href_for(*target, action:, namespace: @namespace)
current = current_page?(href)

link_to_unless(disabled || current, label, href, **turbo!(opts)) do
Expand Down
3 changes: 3 additions & 0 deletions lib/cafe_car/query_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ def parse_value(key, value)
value.exclude_end?)
in Array | Op
value.map { parse_value(key, _1) }
in "true" then true
in "false" then false
in String
case column(key)&.type || reflection(key)&.macro
when :datetime then parse_time(value) || value
Expand Down Expand Up @@ -140,6 +142,7 @@ def association!(name, value, ...)
end

def scope!(name, value)
value = parse_value(name, value)
arity = (@scope.scopes[name] || @scope.method(name)).arity
value = nil if arity == 0 and value == true

Expand Down
2 changes: 1 addition & 1 deletion lib/cafe_car/table/row_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def policy = @template.policy(@object)
def value(method) = present(@object.public_send(method))

def cell(method, *flags, href: nil, **options, &block)
href = @template.url_for(@object) if href == true
href = @template.href_for(@object) if href == true
href = href.to_proc.(@object) if href.respond_to?(:to_proc)

content = block ? capture(value(method), &block) : value(method)
Expand Down
2 changes: 1 addition & 1 deletion test/cafe_car/queryable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class QueryableTest < ActiveSupport::TestCase
end

test "query count" do
refute_empty User.query(articles: 1..5)
refute_empty User.query(articles: 1..5, title: /bob/)
assert_empty User.query(articles: 99)
refute_empty User.query(articles!: 99)
end
Expand Down
5 changes: 5 additions & 0 deletions test/dummy/app/controllers/admin/articles_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Admin
class ArticlesController < ApplicationController
recline_in_the_cafe_car
end
end
5 changes: 5 additions & 0 deletions test/dummy/app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Admin
class UsersController < ApplicationController
recline_in_the_cafe_car
end
end
3 changes: 0 additions & 3 deletions test/dummy/app/controllers/users_controller.rb

This file was deleted.

1 change: 1 addition & 0 deletions test/dummy/app/models/article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Article < ApplicationRecord
scope :draft, -> { where(published_at: Time.zone.now..) }
scope :published, -> { where(published_at: ..Time.zone.now) }
scope :unpublished, -> { where(published_at: nil) }
scope :search, ->(term) { query("title~": term) }

def published? = published_at && published_at < Time.zone.now
def draft? = published_at && published_at > Time.zone.now
Expand Down
4 changes: 4 additions & 0 deletions test/dummy/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ class User < ApplicationRecord
has_secure_password

validates :username, presence: true

def self.search(query)
query("username~": query)
end
end
8 changes: 6 additions & 2 deletions test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
Rails.application.routes.draw do
mount CafeCar::Engine => "/admin"
get "up" => "rails/health#show", as: :rails_health_check

resources :articles
resources :users

namespace :admin do
mount CafeCar::Engine => "/"
resources :articles
resources :users
end

get "*path", to: "pages#show", as: :page
root "pages#show"
Expand Down

0 comments on commit 5be25af

Please sign in to comment.