diff --git a/app/controllers/urls_controller.rb b/app/controllers/urls_controller.rb index c960cef..e662c97 100644 --- a/app/controllers/urls_controller.rb +++ b/app/controllers/urls_controller.rb @@ -29,43 +29,14 @@ def edit end def create - urls = create_shortenURL(params['url'], params['unique_key']) + urls = create_shortenURL(params[:url], params[:unique_key]) @url = urls[:short_url] url = urls[:url] - # This logic all really belongs in the Shortener::ShortenedUrl model. This - # ugliness seems to be a sign that we should fork that gem and extend it. - if url.works? - unless params['unique_key'].blank? - if params['unique_key'] =~ /\A[a-zA-Z0-9]+\Z/ - if Shortener::ShortenedUrl.where(domain_name: @request_domain). - where("lower(unique_key) = ?", params['unique_key'].downcase).exists? - - @url = Shortener::ShortenedUrl.new url: params['url'], domain_name: @request_domain - @url.errors[:base] << 'That short code already exists.' - else - @url = Shortener::ShortenedUrl.generate(url.to_s) - @url.unique_key = params['unique_key'] - @url.domain_name = @request_domain - @url.save - end - else - @url = Shortener::ShortenedUrl.new url: params['url'], domain_name: @request_domain - @url.errors[:base] << 'Short codes can only include numbers and letters.' - end - else - @url = Shortener::ShortenedUrl.generate(url.to_s) - @url.domain_name = @request_domain - end - else - @url = Shortener::ShortenedUrl.new url: params['url'], domain_name: @request_domain - @url.errors[:base] << 'That URL doesn\'t seem to work.' - end - respond_to do |format| if url.works? and @url.errors.messages.empty? - format.html { redirect_to @url } + format.html { redirect_to root_url } format.json { render :show, status: :created, location: @url } else format.html { render :new } diff --git a/app/views/urls/_form.html.haml b/app/views/urls/_form.html.haml index 6ca1922..c6a691e 100644 --- a/app/views/urls/_form.html.haml +++ b/app/views/urls/_form.html.haml @@ -16,6 +16,13 @@ .col-sm-10 = f.text_field :unique_key, class: 'form-control', name: "unique_key", placeholder: "(optional, case insensitive) tiestopromo, nye2014, FREESTUFF", type: "text" + .form-group + %label.col-sm-2.control-label{for: "domain_names"} Domains + .col-sm-10 + - @domains.each do |domain| + = check_box_tag "domain_name[]", domain, @current_domain.nil? || @current_domain == domain, id: "domain_name_input_#{domain}" + = label_tag :domain_name, domain, for: "domain_name_input_#{domain}" + .panel.panel-default .panel-heading Google Analytics UTM parameters (optional) diff --git a/app/views/urls/edit.html.haml b/app/views/urls/edit.html.haml index 30c9d2d..b77547d 100644 --- a/app/views/urls/edit.html.haml +++ b/app/views/urls/edit.html.haml @@ -2,7 +2,7 @@ .panel.panel-default .panel-heading - %h3.panel-title Edit #{domain_name} URL: + %h3.panel-title Edit #{@url.domain_name} URL: .panel-body = render partial: 'form', locals: {f: f} @@ -11,4 +11,5 @@ %btn-toolbar.pull-right %a.btn.btn-default.btn-larget{href: root_path} Cancel %button.btn.btn-primary.btn-large{type: "submit"} Update URL - .clearfix \ No newline at end of file + .clearfix + \ No newline at end of file diff --git a/app/views/urls/new.html.haml b/app/views/urls/new.html.haml index eca77a6..65ad0ec 100644 --- a/app/views/urls/new.html.haml +++ b/app/views/urls/new.html.haml @@ -2,7 +2,7 @@ .panel.panel-default .panel-heading - %h3.panel-title New #{domain_name} URL: + %h3.panel-title New URL: .panel-body = render partial: 'form', locals: {f: f} diff --git a/features/step_definitions/end_user_steps.rb b/features/step_definitions/end_user_steps.rb index 93d407c..a28926d 100644 --- a/features/step_definitions/end_user_steps.rb +++ b/features/step_definitions/end_user_steps.rb @@ -13,6 +13,8 @@ end Then(/^I should be redirected to (.*)$/) do |url| - expect(response.original_headers["Location"]).to eq url - expect(response.status).to eq 301 + VCR.use_cassette("visit_external_url") do + expect(response.original_headers["Location"]).to eq url + expect(response.status).to eq 301 + end end diff --git a/features/step_definitions/url_steps.rb b/features/step_definitions/url_steps.rb index 7551234..b392f25 100644 --- a/features/step_definitions/url_steps.rb +++ b/features/step_definitions/url_steps.rb @@ -17,3 +17,7 @@ Then(/^I should have (\d+) Shorted URL with domain name as "([^"]*)"$/) do |count, domain_name| expect(Shortener::ShortenedUrl.where(domain_name: domain_name).count).to eq count.to_i end + +Then /^I should see "([^\"]*)" domain checkbox as checked$/ do |domain| + expect(response).to have_selector("input", id: "domain_name_input_#{domain}", checked: "checked") +end diff --git a/features/step_definitions/webrat_steps.rb b/features/step_definitions/webrat_steps.rb index 5e07048..b655dae 100644 --- a/features/step_definitions/webrat_steps.rb +++ b/features/step_definitions/webrat_steps.rb @@ -34,5 +34,5 @@ end Then /^I should see "([^\"]*)" in (.*)$/ do |text, selector_context| - expect(response).to have_selector(selector_context_to_id_map[selector_context], :content => text) + expect(response).to have_selector(selector_context_to_id_map[selector_context], content: text) end diff --git a/features/url_management.feature b/features/url_management.feature index de428eb..04c49c3 100644 --- a/features/url_management.feature +++ b/features/url_management.feature @@ -6,14 +6,18 @@ Feature: URL Management Scenario: Admin logs into the dashboard and creates a short URL Given Our host is "hkk.sn" And I am an authorized user with credentials as name and password + And there is a shortened URL http://omnia.ws/blah1 that goes to http://example1.com/ When I go to homepage and provide name and password Then I should see "Custom branded short URLs" And I should see "Create new short URL" When I click "Create new short URL" - Then I should see "New hkk.sn URL" + Then I should see "New URL" + And I should see "hkk.sn" domain checkbox as checked + And I should see "omnia.ws" domain checkbox as checked When I enter "http://external.com" into the input box as the URL in new url form And I enter "blah" into the input box as the short code in new url form And I submit the new url form - Then I should have 1 Shorted URL with domain name as "hkk.sn" + Then I should have 2 Shorted URL with domain name as "hkk.sn" When I go to homepage Then I should see "hkk.sn/blah" + And I should see "omnia.ws/blah"