lib/voom/presenters/web_client/router.rb in voom-presenters-0.1.13 vs lib/voom/presenters/web_client/router.rb in voom-presenters-0.2.0

- old
+ new

@@ -9,86 +9,51 @@ def initialize(base_url: nil) @base_url = base_url end - def url(command: nil, render: nil, entity: nil, context:) + def url(command: nil, render: nil, context:) _params_ = context.dup - # if it is a local page command - # return build_local_render(command, render, entity, _params_) if local_render?(render) - return build_render_url(render, entity, _params_) unless command - _params_[:redirect]=build_render_url(render, entity, _params_) if render - build_command_url(command, entity, _params_) + return build_render_url(render, _params_) unless command + _params_[:redirect]=build_render_url(render, _params_) if render + build_command_url(command, _params_) end - def local_render?(render) - return unless render - render.to_s.start_with?('#') - end - - def build_local_render(command, render, entity, context) - - client_command = render[/(.*?)\(/m, 1] - - start_params = render.index('(') - end_params = render.rindex(')') - client_params = render[start_params+1..end_params-1] - client_params = client_params.split(',') - - client_command += '(' - client_params.each.with_index do |p, index| - client_command += ',' if index > 0 - url_var = p[/{url\((.*?)\)}/m, 1] - client_command += build_client_url(url_var.strip, entity, context) if url_var && command.nil? - client_command += p.strip unless url_var - end - client_command += ')' - end - - def join_errors - '<br/>' - end - def scrub_params(_params_) - # _params_.delete('captures') - # _params_.delete('render') - # _params_.delete('view') + _params_.delete('captures') _params_.delete('presenter') - # _params_.delete('redirect') _params_.delete('action') _params_.delete('errors') _params_ end private - def build_command_url(command, entity, params) + def build_command_url(command, params) return '' unless command - seperator = command.include?('?') ? '&' : '?' - "#{command}#{seperator}#{build_params(params, entity)}" + add_query_params(command, params) end - def build_render_url(render_, entity, params) + def build_render_url(render_, params) return '#' unless render_ render = render_.to_s return render if render.start_with?('http') + render = render.gsub(':', '/') seperator = render.start_with?('/') ? '' : '/' url = "#{base_url}#{seperator}#{render}" - query_params = build_params(params, entity) - if(query_params) - query_seperator = render.include?('?') ? '&' : '?' + add_query_params(url, params) + end + + def add_query_params(url, params) + query_params = build_params(params) + if (!query_params.nil? && !query_params.empty?) + query_seperator = url.include?('?') ? '&' : '?' url = "#{url}#{query_seperator}#{query_params}" end url end - def build_client_url(render, entity, params) - return '#' unless render - "#{base_url}/web/#{render}?layout=false&#{build_params(params, entity)}" - end - - def build_params(params, entity) - params = params.merge(entity_key: entity.id) if entity + def build_params(params) Rack::Utils.build_nested_query(scrub_params(params)) end end end end \ No newline at end of file