Skip to content

Commit

Permalink
Merge pull request #15373 from imtayadeway/api/decouple-from-compress…
Browse files Browse the repository at this point in the history
…ed-ids-helper

Decouple from CompressedIds mixin
  • Loading branch information
Fryguy authored Jun 14, 2017
2 parents 087e18e + e4d8027 commit 9fdcde9
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/automate_domains_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def automate_domain_ident(domain)
end

def resource_search(id, type, klass)
if cid?(id)
if ApplicationRecord.compressed_id?(id)
super
else
begin
Expand Down
1 change: 0 additions & 1 deletion app/controllers/api/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class BaseController < ActionController::API
include_concern 'Results'
include_concern 'Generic'
include_concern 'Authentication'
include CompressedIds
include ActionController::HttpAuthentication::Basic::ControllerMethods
extend ErrorHandler::ClassMethods

Expand Down
10 changes: 5 additions & 5 deletions app/controllers/api/base_controller/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def href_collection_id(path)
collection, c_id = path_array[cidx..cidx + 1]
subcollection, s_id = path_array[cidx + 2..cidx + 3]

subcollection ? [subcollection.to_sym, from_cid(s_id)] : [collection.to_sym, from_cid(c_id)]
subcollection ? [subcollection.to_sym, ApplicationRecord.uncompress_id(s_id)] : [collection.to_sym, ApplicationRecord.uncompress_id(c_id)]
end

def parse_id(resource, collection)
Expand All @@ -88,14 +88,14 @@ def parse_id(resource, collection)
href_id
when resource["id"].kind_of?(Integer)
resource["id"]
when cid?(resource["id"])
from_cid(resource["id"])
when ApplicationRecord.compressed_id?(resource["id"])
ApplicationRecord.uncompress_id(resource["id"])
end
end

def href_id(href, collection)
if href.present? && href.match(%r{^.*/#{collection}/(#{BaseController::CID_OR_ID_MATCHER})$})
from_cid(Regexp.last_match(1))
if href.present? && href.match(%r{^.*/#{collection}/(#{ApplicationRecord::CID_OR_ID_MATCHER})$})
ApplicationRecord.uncompress_id(Regexp.last_match(1))
end
end

Expand Down
6 changes: 2 additions & 4 deletions app/controllers/api/base_controller/parser/request_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module Api
class BaseController
module Parser
class RequestAdapter
include CompressedIds

def initialize(req, params)
@request = req
@params = params
Expand Down Expand Up @@ -68,7 +66,7 @@ def c_suffix

def c_id
id = @params[:c_id] || c_path_parts[1]
cid?(id) ? from_cid(id) : id
ApplicationRecord.compressed_id?(id) ? ApplicationRecord.uncompress_id(id) : id
end

def subcollection
Expand All @@ -77,7 +75,7 @@ def subcollection

def s_id
id = @params[:s_id] || c_path_parts[3]
cid?(id) ? from_cid(id) : id
ApplicationRecord.compressed_id?(id) ? ApplicationRecord.uncompress_id(id) : id
end

def subcollection?
Expand Down
6 changes: 5 additions & 1 deletion app/controllers/api/subcollections/policies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ def parse_policy(data, collection, klass)
return klass.find_by(:guid => guid) if guid.present?

href = data["href"]
href =~ %r{^.*/#{collection}/#{BaseController::CID_OR_ID_MATCHER}$} ? klass.find(from_cid(href.split('/').last)) : {}
if href =~ %r{^.*/#{collection}/#{ApplicationRecord::CID_OR_ID_MATCHER}$}
klass.find(ApplicationRecord.uncompress_id(href.split('/').last))
else
{}
end
end

def policy_subcollection_action(ctype, policy)
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/api/subcollections/tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ def parse_tag(data)

def parse_tag_from_href(data)
href = data["href"]
tag = if href && href.match(%r{^.*/tags/#{BaseController::CID_OR_ID_MATCHER}$})
tag = if href && href.match(%r{^.*/tags/#{ApplicationRecord::CID_OR_ID_MATCHER}$})
klass = collection_class(:tags)
klass.find(from_cid(href.split('/').last))
klass.find(ApplicationRecord.uncompress_id(href.split('/').last))
end
tag.present? ? tag_path_to_spec(tag.name).merge(:id => tag.id) : {}
end
Expand Down
6 changes: 3 additions & 3 deletions lib/api/filter.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module Api
class Filter
include CompressedIds

OPERATORS = {
"!=" => {:default => "!=", :regex => "REGULAR EXPRESSION DOES NOT MATCH", :null => "IS NOT NULL"},
"<=" => {:default => "<="},
Expand Down Expand Up @@ -82,7 +80,9 @@ def parse_filter(filter)
end
end

filter_value = from_cid(filter_value) if filter_attr =~ /[_]?id$/ && cid?(filter_value)
if filter_attr =~ /[_]?id$/ && ApplicationRecord.compressed_id?(filter_value)
filter_value = ApplicationRecord.uncompress_id(filter_value)
end

if filter_value =~ /%|\*/
filter_value = "/\\A#{Regexp.escape(filter_value)}\\z/"
Expand Down

0 comments on commit 9fdcde9

Please sign in to comment.