Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/controllers/sites_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ def new
@site = Site.new
end

def index
@sites = Site.all
end

def create
@site = Site.new(site_params)
if @site.save
Expand Down
39 changes: 39 additions & 0 deletions app/models/site.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,41 @@
require 'resolv'
class Site < ActiveRecord::Base
validates_presence_of :name, :url, :creator
validates_length_of :name, :minimum => 3, :maximum => 100
validates_length_of :url, :minimum => 3, :maximum => 100
validates_length_of :description, :minimum => 0, :maxmum => 1000
validates_length_of :creator, :minimum =>1, :maximum => 100
validates_length_of :hash_tag, :minimum =>0, :maximum => 140
validates_length_of :repository_url, :minimum => 3, :maximum => 100
validates_uniqueness_of :url, :repository_url
validate :url_is_heroku?

private
def url_is_heroku?
return if self.url =~ /heroku(app)?\.com\/?/
return true if Rails.env.test?
if self.url =~ /http(?:s)?:\/\/([^\/]+)/
host = $1
begin
ipaddress = Resolv.getaddress host
rescue => e
logger.error e.message
errors.add :url, ' Invalid heroku URL'
return
end
unless APP_CONFIG[:heroku][:custom_domain].include? ipaddress
begin
cname = Resolv::DNS.new.getresource(host, Resolv::DNS::Resource::IN::ANY).name.to_s
rescue => e
logger.error e.message
errors.add :url, ' Invalid heroku URL'
return
end
unless cname =~ /heroku(app|ssl)?\.com\/?/
errors.add :url, ' Invalid heroku URL'
end
end
end
end

end
6 changes: 6 additions & 0 deletions app/views/sites/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
%h1 Site list

%ul.sites
- @sites.each do |site|
%li
%h2= site.name
2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ class Application < Rails::Application

config.action_mailer.default_url_options = { host: Figaro.env.host }
end
end
end
37 changes: 37 additions & 0 deletions config/initializers/extensions/validates_url_format_of.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# encoding: utf-8
module ValidatesUrlFormatOf
IPv4_PART = /\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]/ # 0-255

# First regexp doesn't work in Ruby 1.8 and second has a bug in 1.9.2:
# https://github.com/henrik/validates_url_format_of/issues/issue/4/#comment_760674
ALNUM = "ä".match(/[[:alnum:]]/) ? /[[:alnum:]]/ : /[^\W_]/

REGEXP = %r{
\A
https?:// # http:// or https://
([^\s:@]+:[^\s:@]*@)? # optional username:pw@
( ((#{ALNUM}+\.)*xn--)?#{ALNUM}+([-.]#{ALNUM}+)*\.[a-z]{2,6}\.? | # domain (including Punycode/IDN)...
#{IPv4_PART}(\.#{IPv4_PART}){3} ) # or IPv4
(:\d{1,5})? # optional port
([/?]\S*)? # optional /whatever or ?whatever
\Z
}iux

DEFAULT_MESSAGE = 'does not appear to be a valid URL'
DEFAULT_MESSAGE_URL = 'does not appear to be valid'

def validates_url_format_of(*attr_names)
options = { :allow_nil => false,
:allow_blank => false,
:with => REGEXP }
options = options.merge(attr_names.pop) if attr_names.last.is_a?(Hash)

attr_names.each do |attr_name|
message = attr_name.to_s.match(/(_|\b)URL(_|\b)/i) ? DEFAULT_MESSAGE_URL : DEFAULT_MESSAGE
validates_format_of(attr_name, { :message => message }.merge(options))
end
end

end

ActiveRecord::Base.extend(ValidatesUrlFormatOf)
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
WeLoveHeroku::Application.routes.draw do
resources :sites, only: [:new, :create, :show]
resources :sites, only: [:new, :create, :index, :show]
end
1 change: 1 addition & 0 deletions spec/factories/sites.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
name Faker::Name.name
url Faker::Internet.url
description Faker::Lorem.paragraph
creator Faker::Name.name
hash_tag '#twitter_hash_tag'
repository_url 'http://github.com/herokai/weloveheroku'
scheduled_access false
Expand Down
19 changes: 19 additions & 0 deletions spec/features/sites_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
require 'spec_helper'
describe "Sites" do

describe 'サイト一覧画面へアクセスする' do
let!(:site) { FactoryGirl.create(:site) }
before do
visit sites_path
end
it '画面が表示されること' do
page.should have_content('Site list')
end

it '登録されたサイトが表示されること' do
within('ul.sites') do
page.should have_content(site.name)
end
end

end


describe 'サイト登録画面へアクセスする' do
before do
visit new_site_path
Expand Down
2 changes: 1 addition & 1 deletion spec/routing/sites_routing_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'spec_helper'
describe SitesController do
resources_should_routes 'sites', [:new, :create, :show]
resources_should_routes 'sites', [:new, :create, :index, :show]
end