Files
fizzy/app/models/command/parser/context.rb
T
Jorge Manrubia bd7b9a442e Merge branch 'main' into fizzy-do-prompts
* main: (57 commits)
  Untenanted access in authenticated controllers should request auth
  dep: bundle update
  dep: update Rails
  dep: bump AR::Tenanted
  Clean up the cable meta tag
  Fix activity feed pagination to fetch on demand
  Create a "published" event if a card is created published
  Scatter Honcho seeds over a 30 day period
  Fix the PWA manifest to use slugged URLs
  Collection publication edits render a turbo frame
  Collection workflow edits render a turbo frame
  Collection entropy config edits render a turbo frame
  Use automatic sizing where supported
  Introduce a separate controller for collection entropy config
  Add `card_id` to index
  Move to scope and inline method
  Prefer modern syntax
  Fix that stage should be visible above card background
  Indicate opens menu
  Hide dialog targets from screen readers when closed
  ...

# Conflicts:
#	db/schema.rb
#	db/schema_cache.yml
2025-07-03 16:11:00 +02:00

96 lines
2.4 KiB
Ruby

class Command::Parser::Context
attr_reader :user, :url, :script_name
def initialize(user, url:, script_name: "")
@user = user
@url = url
@script_name = script_name
extract_url_components
end
def cards
if viewing_card_contents?
user.accessible_cards.where id: params[:id]
elsif viewing_cards_index?
filter.cards.published
elsif viewing_search_results?
user.accessible_cards.where(id: user.search(params[:q]).select(:card_id))
else
Card.none
end
end
def viewing_card_contents?
viewing_card_perma?
end
def viewing_list_of_cards?
viewing_cards_index? || viewing_search_results?
end
def find_user(string)
string = string.delete_prefix("@")
if string.starts_with?("gid://")
User.find_by_id(GlobalID::Locator.locate(string).id)
else
User.all.find { |user| user.mentionable_handles.include?(string.downcase) }
end
end
def find_workflow_stage(string)
candidate_stages.find do |stage|
stage.name.downcase.include?(string.downcase)
end
end
def find_tag(string)
string = string.delete_prefix("#")
if string.starts_with?("gid://")
Tag.find_by_id(GlobalID::Locator.locate(string).id)
else
Tag.find_by_title(string)
end
end
def find_collection(string)
Collection.where("lower(name) like ?", "%#{string.downcase}%").first
end
private
attr_reader :controller, :action, :params
MAX_CARDS = 20
MAX_CLOSED_CARDS = 10
def filter
user.filters.from_params(params.permit(*Filter::Params::PERMITTED_PARAMS).reverse_merge(**FilterScoped::DEFAULT_PARAMS))
end
def viewing_card_perma?
controller == "cards" && action == "show"
end
def viewing_cards_index?
controller == "cards" && action == "index"
end
def viewing_search_results?
controller == "searches" && action == "show"
end
def extract_url_components
uri = URI.parse(url || "")
path = uri.path.delete_prefix(script_name)
route = Rails.application.routes.recognize_path(path)
@controller = route[:controller]
@action = route[:action]
@params = ActionController::Parameters.new(Rack::Utils.parse_nested_query(uri.query).merge(route.except(:controller, :action)))
end
def candidate_stages
Workflow::Stage.where(workflow_id: cards.joins(:collection).select("collections.workflow_id").distinct)
end
end