router_constraints.rb revision 419f986abb76f6fce54b71e17f52a1deaa06dbd6
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenclass RouterConstraint
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen def set_path_parameters(request, new_params)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen params = request.send(:env)["action_dispatch.request.path_parameters"]
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen controller = params[:controller]
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen action = params[:action]
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen params.except!(*params.keys).merge!(
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen controller: controller,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen action: action).merge!(new_params)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen end
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenend
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainenclass FilesRouterConstraint < RouterConstraint
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen def matches?(request)
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen return false if Repository.find_by_path(request.params[:repository_id]).nil?
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen result = !RepositoryFile.find_with_path(
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen params_path_without_format(request)).nil?
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen set_params_path_without_format(request) if result
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen return result
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen end
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen protected
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen def params_path_without_format(request)
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen @params = request.send(:env)["action_dispatch.request.path_parameters"]
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen @path = @params[:path]
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen @path += ".#{@params[:format]}" if @params[:format]
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen @params.merge({ path: @path }).except(:format)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen end
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen def set_params_path_without_format(request)
16f816d3f3c32ae3351834253f52ddd0212bcbf3Timo Sirainen params_path_without_format(request)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen @params.merge!({ path: @path }).except!(:format)
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen end
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenend
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainenclass IRIRouterConstraint < RouterConstraint
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen def matches?(request, path=nil)
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen path ||= request.original_fullpath
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ontology = Ontology.find_with_iri(path)
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen result = !ontology.nil?
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen if result
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen set_path_parameters(request,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen repository_id: ontology.repository.to_param, id: ontology.id)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen end
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen
7a6b45405fb1544ac476e6eb1402a70cc1ddcdcfTimo Sirainen return result
e05ea8311ae16687295048e88ca205dfe29fbcbfTimo Sirainen end
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainenend
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainenclass RefIRIRouterConstraint < IRIRouterConstraint
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen def matches?(request)
f7d43647acc6dc80064c8c4cacf5bf86f754c530Timo Sirainen # remove the ref/:version_number portion from path
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen path = request.original_fullpath.sub(%r{\A/ref/\d+/}, '')
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen super(request, path)
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen end
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainenend
98c1cf256927e254f0c092acd2ddcd7ea50bd009Timo Sirainen
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainenclass MIMERouterConstraint < RouterConstraint
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen attr_accessor :mime_types
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen
8153fdec343e40e2a78f5c12353e89b994b28f74Timo Sirainen def initialize(*mime_types)
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen self.mime_types = mime_types.flatten.map { |m| Mime::Type.lookup(m) }
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen super()
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen end
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen def matches?(request)
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen mime_types.any? { |m| request.accepts.first == m }
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen end
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainenend
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainenclass GroupedConstraint
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen attr_accessor :constraints
39775ad03c459efe64cce924658da5094ba417e1Timo Sirainen
39775ad03c459efe64cce924658da5094ba417e1Timo Sirainen def initialize(*args)
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen self.constraints = args.flatten
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen end
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen
39775ad03c459efe64cce924658da5094ba417e1Timo Sirainen def matches?(request)
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen constraints.all? { |c| c.matches?(request) }
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen end
445f9e31c6c3aa6c0a72be8565da8f6e594d24fbTimo Sirainenend
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen