مراية لـ
https://github.com/postalserver/postal.git
تم المزامنة 2025-11-30 21:32:30 +00:00
refactor: move Postal::QueryString to app/util/query_string
هذا الالتزام موجود في:
@@ -161,7 +161,7 @@ class MessagesController < ApplicationController
|
|||||||
|
|
||||||
if @query = (params[:query] || session["msg_query_#{@server.id}_#{scope}"]).presence
|
if @query = (params[:query] || session["msg_query_#{@server.id}_#{scope}"]).presence
|
||||||
session["msg_query_#{@server.id}_#{scope}"] = @query
|
session["msg_query_#{@server.id}_#{scope}"] = @query
|
||||||
qs = Postal::QueryString.new(@query)
|
qs = QueryString.new(@query)
|
||||||
if qs.empty?
|
if qs.empty?
|
||||||
flash.now[:alert] = "It doesn't appear you entered anything to filter on. Please double check your query."
|
flash.now[:alert] = "It doesn't appear you entered anything to filter on. Please double check your query."
|
||||||
else
|
else
|
||||||
|
|||||||
36
app/util/query_string.rb
Normal file
36
app/util/query_string.rb
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class QueryString
|
||||||
|
|
||||||
|
def initialize(string)
|
||||||
|
@string = string.strip + " "
|
||||||
|
end
|
||||||
|
|
||||||
|
def [](value)
|
||||||
|
hash[value.to_s]
|
||||||
|
end
|
||||||
|
|
||||||
|
delegate :empty?, to: :hash
|
||||||
|
|
||||||
|
def hash
|
||||||
|
@hash ||= @string.scan(/([a-z]+):\s*(?:(\d{2,4}-\d{2}-\d{2}\s\d{2}:\d{2})|"(.*?)"|(.*?))(\s|\z)/).each_with_object({}) do |(key, date, string_with_spaces, value), hash|
|
||||||
|
if date
|
||||||
|
actual_value = date
|
||||||
|
elsif string_with_spaces
|
||||||
|
actual_value = string_with_spaces
|
||||||
|
elsif value == "[blank]"
|
||||||
|
actual_value = nil
|
||||||
|
else
|
||||||
|
actual_value = value
|
||||||
|
end
|
||||||
|
|
||||||
|
if hash.keys.include?(key.to_s)
|
||||||
|
hash[key.to_s] = [hash[key.to_s]].flatten
|
||||||
|
hash[key.to_s] << actual_value
|
||||||
|
else
|
||||||
|
hash[key.to_s] = actual_value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
module Postal
|
|
||||||
class QueryString
|
|
||||||
|
|
||||||
def initialize(string)
|
|
||||||
@string = string.strip + " "
|
|
||||||
end
|
|
||||||
|
|
||||||
def [](value)
|
|
||||||
hash[value.to_s]
|
|
||||||
end
|
|
||||||
|
|
||||||
delegate :empty?, to: :hash
|
|
||||||
|
|
||||||
def hash
|
|
||||||
@hash ||= @string.scan(/([a-z]+):\s*(?:(\d{2,4}-\d{2}-\d{2}\s\d{2}:\d{2})|"(.*?)"|(.*?))(\s|\z)/).each_with_object({}) do |(key, date, string_with_spaces, value), hash|
|
|
||||||
if date
|
|
||||||
actual_value = date
|
|
||||||
elsif string_with_spaces
|
|
||||||
actual_value = string_with_spaces
|
|
||||||
elsif value == "[blank]"
|
|
||||||
actual_value = nil
|
|
||||||
else
|
|
||||||
actual_value = value
|
|
||||||
end
|
|
||||||
|
|
||||||
if hash.keys.include?(key.to_s)
|
|
||||||
hash[key.to_s] = [hash[key.to_s]].flatten
|
|
||||||
hash[key.to_s] << actual_value
|
|
||||||
else
|
|
||||||
hash[key.to_s] = actual_value
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
require "rails_helper"
|
|
||||||
|
|
||||||
describe Postal::QueryString do
|
|
||||||
it "should work with a single item" do
|
|
||||||
qs = Postal::QueryString.new("to: test@example.com")
|
|
||||||
expect(qs.hash["to"]).to eq "test@example.com"
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should work with a multiple items" do
|
|
||||||
qs = Postal::QueryString.new("to: test@example.com from: another@example.com")
|
|
||||||
expect(qs.hash["to"]).to eq "test@example.com"
|
|
||||||
expect(qs.hash["from"]).to eq "another@example.com"
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should not require a space after the field name" do
|
|
||||||
qs = Postal::QueryString.new("to:test@example.com from:another@example.com")
|
|
||||||
expect(qs.hash["to"]).to eq "test@example.com"
|
|
||||||
expect(qs.hash["from"]).to eq "another@example.com"
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should return nil when it receives blank" do
|
|
||||||
qs = Postal::QueryString.new("to:[blank]")
|
|
||||||
expect(qs.hash["to"]).to eq nil
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should handle dates with spaces" do
|
|
||||||
qs = Postal::QueryString.new("date: 2017-02-12 15:20")
|
|
||||||
expect(qs.hash["date"]).to eq("2017-02-12 15:20")
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should return an array for multiple items" do
|
|
||||||
qs = Postal::QueryString.new("to: test@example.com to: another@example.com")
|
|
||||||
expect(qs.hash["to"]).to be_a(Array)
|
|
||||||
expect(qs.hash["to"][0]).to eq "test@example.com"
|
|
||||||
expect(qs.hash["to"][1]).to eq "another@example.com"
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should work with a z in the string" do
|
|
||||||
qs = Postal::QueryString.new("to: testaz@example.com")
|
|
||||||
expect(qs.hash["to"]).to eq "testaz@example.com"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
44
spec/util/query_string_spec.rb
Normal file
44
spec/util/query_string_spec.rb
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require "rails_helper"
|
||||||
|
|
||||||
|
describe QueryString do
|
||||||
|
it "works with a single item" do
|
||||||
|
qs = described_class.new("to: test@example.com")
|
||||||
|
expect(qs.hash["to"]).to eq "test@example.com"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "works with a multiple items" do
|
||||||
|
qs = described_class.new("to: test@example.com from: another@example.com")
|
||||||
|
expect(qs.hash["to"]).to eq "test@example.com"
|
||||||
|
expect(qs.hash["from"]).to eq "another@example.com"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "does not require a space after the field name" do
|
||||||
|
qs = described_class.new("to:test@example.com from:another@example.com")
|
||||||
|
expect(qs.hash["to"]).to eq "test@example.com"
|
||||||
|
expect(qs.hash["from"]).to eq "another@example.com"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns nil when it receives blank" do
|
||||||
|
qs = described_class.new("to:[blank]")
|
||||||
|
expect(qs.hash["to"]).to eq nil
|
||||||
|
end
|
||||||
|
|
||||||
|
it "handles dates with spaces" do
|
||||||
|
qs = described_class.new("date: 2017-02-12 15:20")
|
||||||
|
expect(qs.hash["date"]).to eq("2017-02-12 15:20")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns an array for multiple items" do
|
||||||
|
qs = described_class.new("to: test@example.com to: another@example.com")
|
||||||
|
expect(qs.hash["to"]).to be_a(Array)
|
||||||
|
expect(qs.hash["to"][0]).to eq "test@example.com"
|
||||||
|
expect(qs.hash["to"][1]).to eq "another@example.com"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "works with a z in the string" do
|
||||||
|
qs = described_class.new("to: testaz@example.com")
|
||||||
|
expect(qs.hash["to"]).to eq "testaz@example.com"
|
||||||
|
end
|
||||||
|
end
|
||||||
المرجع في مشكلة جديدة
حظر مستخدم