From e0efe996b01e8d62d394eccd2bce42c6b261e863 Mon Sep 17 00:00:00 2001 From: Kyle Drake Date: Sun, 8 Jun 2014 18:32:33 -0700 Subject: [PATCH] refactor tags, whitespace --- .gitignore | 1 + app.rb | 8 ++---- models/site.rb | 51 ++++++++++++++++++++++++++++++--------- tests/acceptance_tests.rb | 47 ++++++++++++++++++++++++++++++++---- tests/api_tests.rb | 2 +- views/new.erb | 44 ++++++++++++++++----------------- 6 files changed, 107 insertions(+), 46 deletions(-) diff --git a/.gitignore b/.gitignore index 06044c8d..d9e773be 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,4 @@ public/assets/css/.sass-cache/ public/site_thumbnails public/sites public/site_screenshots +*.swp diff --git a/app.rb b/app.rb index f92cdd20..3925d848 100644 --- a/app.rb +++ b/app.rb @@ -99,10 +99,6 @@ post '/site/:sitename/comment' do |sitename| redirect "/site/#{sitename}" end -get '/tags_mockup' do - erb :'tags_mockup' -end - get '/browse_mockup' do erb :'browse_mockup' end @@ -225,7 +221,7 @@ end post '/tags/add' do require_login - current_site.new_tags = params[:tags] + current_site.new_tags_string = params[:tags] current_site.save validate: false redirect request.referer end @@ -299,7 +295,7 @@ post '/create' do username: params[:username], password: params[:password], email: params[:email], - new_tags: params[:tags], + new_tags_string: params[:tags], is_nsfw: params[:is_nsfw], ip: request.ip ) diff --git a/models/site.rb b/models/site.rb index 4c20b76f..e2a6c931 100644 --- a/models/site.rb +++ b/models/site.rb @@ -138,12 +138,8 @@ class Site < Sequel::Model values[:password] = BCrypt::Password.create plaintext, cost: (self.class.bcrypt_cost || BCrypt::Engine::DEFAULT_COST) end - def new_tags=(tags_string) - tags_string.gsub! ', ', ',' - tags = tags_string.split ',' - tags_string.gsub! /[^a-zA-Z0-9, ]/, '' - tags.collect! {|c| (c.match(/^\w+\s\w+/) || c.match(/^\w+/)).to_s } - @new_tag_strings = tags.sort + def new_tags_string=(tags_string) + @new_tags_string = tags_string end def before_validation @@ -213,7 +209,7 @@ class Site < Sequel::Model end def store_file(filename, uploaded) - if File.exist?(file_path(filename)) && + if File.exist?(file_path(filename)) && Digest::SHA2.file(file_path(filename)).digest == Digest::SHA2.file(uploaded.path).digest return false end @@ -287,18 +283,18 @@ class Site < Sequel::Model end def after_save - if @new_tag_strings - @new_tag_strings.each do |new_tag_string| + if @new_filtered_tags + @new_filtered_tags.each do |new_tag_string| add_tag_name new_tag_string end + @new_filtered_tags = [] + @new_tags_string = nil end super end def add_tag_name(name) - if tags_dataset.filter(name: name).first.nil? - add_tag Tag[name: name] || Tag.create(name: name) - end + add_tag Tag[name: name] || Tag.create(name: name) end def after_create @@ -350,6 +346,37 @@ class Site < Sequel::Model errors.add :domain, "Domain provided is already being used by another site, please choose another." end end + + if @new_tags_string + new_tags = @new_tags_string.split ',' + new_tags.uniq! + new_tags.compact! + @new_filtered_tags = [] + + if new_tags.length > 5 + error.add :tags, 'Cannot have more than 5 tags.' + end + + new_tags.each do |tag| + tag.strip! + if tag.match(/[^a-zA-Z0-9 ]/) + errors.add :tags, "Tag \"#{tag}\" can only contain letters (A-Z) and numbers (0-9)." + break + end + + if tag.match(/ /) + errors.add :tags, "Tag \"#{tag}\" cannot have more than one space between words." + break + end + + if tag.split(' ').length > 2 + errors.add :tags, "Tag \"#{tag}\" cannot be more than two words." + break + end + + @new_filtered_tags << tag + end + end end def render_template(name) diff --git a/tests/acceptance_tests.rb b/tests/acceptance_tests.rb index 3e0f8ca5..1baac0df 100644 --- a/tests/acceptance_tests.rb +++ b/tests/acceptance_tests.rb @@ -84,13 +84,50 @@ describe 'signup' do click_button 'Create Home Page' page.must_have_content 'A valid user/site name is required' end - + it 'fails with username greater than 32 characters' do fill_in_valid fill_in 'username', with: SecureRandom.hex+'1' click_button 'Create Home Page' page.must_have_content 'cannot exceed 32 characters' end + + it 'fails with invalid tag chars' do + fill_in_valid + fill_in 'tags', with: '$POLICE OFFICER$$$$$, derp' + click_button 'Create Home Page' + page.must_have_content /Tag.+can only contain/ + end + + it 'fails for tag with too many spaces' do + fill_in_valid + fill_in 'tags', with: 'police officer, hi' + click_button 'Create Home Page' + page.must_have_content /Tag.+cannot have more than one space/ + end + + it 'succeeds with no tags' do + fill_in_valid + fill_in 'tags', with: '' + click_button 'Create Home Page' + page.must_have_content 'Your Feed' + end + + it 'succeeds with a single tag' do + fill_in_valid + fill_in 'tags', with: 'derpie' + click_button 'Create Home Page' + page.must_have_content 'Your Feed' + Site.last.tags.first.name.must_equal 'derpie' + end + + it 'succeeds with valid tags' do + fill_in_valid + fill_in 'tags', with: 'derpie, shoujo manga' + click_button 'Create Home Page' + page.must_have_content 'Your Feed' + Site.last.tags.collect {|t| t.name}.must_equal ['derpie', 'shoujo manga'] + end end describe 'signin' do @@ -105,7 +142,7 @@ describe 'signin' do before do Capybara.reset_sessions! end - + it 'fails for invalid login' do visit '/' click_link 'Sign In' @@ -114,7 +151,7 @@ describe 'signin' do click_button 'Sign In' page.must_have_content 'Invalid login' end - + it 'fails for missing login' do visit '/' click_link 'Sign In' @@ -124,7 +161,7 @@ describe 'signin' do click_button 'Sign In' page.must_have_content 'Invalid login' end - + it 'logs in with proper credentials' do visit '/' click_button 'Create My Website' @@ -140,4 +177,4 @@ describe 'signin' do click_button 'Sign In' page.must_have_content 'Your Feed' end -end \ No newline at end of file +end diff --git a/tests/api_tests.rb b/tests/api_tests.rb index 916242fc..a6bb491a 100644 --- a/tests/api_tests.rb +++ b/tests/api_tests.rb @@ -34,7 +34,7 @@ describe 'api info' do it 'succeeds for valid sitename' do create_site - @site.update hits: 31337, domain: 'derp.com', new_tags: 'derpie, man' + @site.update hits: 31337, domain: 'derp.com', new_tags_string: 'derpie, man' get '/api/info', sitename: @user res[:result].must_equal 'success' res[:info][:sitename].must_equal @site.username diff --git a/views/new.erb b/views/new.erb index d2f50e40..52d9f931 100644 --- a/views/new.erb +++ b/views/new.erb @@ -27,7 +27,7 @@ <% end %> - +

First, enter a username. This will also be used as your site name.
Do not forget this, it will be used to sign in to and manage your home page. It can only contain letters, numbers, underscores and hyphens, and can only be 32 characters long.

@@ -35,7 +35,7 @@

.neocities.org

- +

@@ -61,19 +61,19 @@


-

You can optionally enter some tags! Tags will allow others to find your site based on your interests, or your site's theme. Separate multiple tags with commas. Don't think too hard about this, you can change them later. You can have a maximum of ten tags, and there is a two word per tag maximum (extra words in a tag will be removed).

- +

You can optionally enter some tags! Tags will allow others to find your site based on your interests, or your site's theme. Separate multiple tags with commas. Don't think too hard about this, you can change them later. You can have a maximum of five tags, and tags can only contain letters (A-Z) and numbers (0-9). There is a two word per tag maximum.

+
Tags
- +
- +

The site you are creating will be free, forever. We will never charge you for your web site.

Neocities has to pay the bills though, and we like the idea of being able to work on the site full-time someday. So if you would like to help us reach this goal, we have created the Supporter Plan! - +

Right now, the Supporter Plan is the same as the free plan, except that Supporter Plan members get 200MB of web space. You will also be listed as a supporter on our contributors page, and on your site profile page.

The base plan is $12 ($1/month) billed once per year, which is the cost of a delicious Yafa Combo with a lousy tip. If you ever decide to cancel, you get to keep the extra space. Thanks for helping us run this site!

- +
> Free Plan (<%= Site::FREE_MAXIMUM_IN_MEGABYTES %>MB) @@ -83,7 +83,7 @@ > Supporter Plan (<%= Site::SUPPORTER_MAXIMUM_IN_MEGABYTES %>MB)
- + - +
- +

Last thing! Enter these two words correctly (with spaces) so we know you're not a robot (don't worry robots, we still love you).

- +
<%== recaptcha_tag :challenge, ssl: request.ssl? %>
- +
- +

You're done. Just click the button below!

- + \ No newline at end of file +