Skip to content

Commit

Permalink
url form changes, feature spec for multiple domains added
Browse files Browse the repository at this point in the history
  • Loading branch information
kevivmatrix committed Mar 11, 2016
1 parent 629f833 commit 1633f6e
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 39 deletions.
33 changes: 2 additions & 31 deletions app/controllers/urls_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
7 changes: 7 additions & 0 deletions app/views/urls/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions app/views/urls/edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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
.clearfix

2 changes: 1 addition & 1 deletion app/views/urls/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
6 changes: 4 additions & 2 deletions features/step_definitions/end_user_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions features/step_definitions/url_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion features/step_definitions/webrat_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 6 additions & 2 deletions features/url_management.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 1633f6e

Please sign in to comment.