refactor tags, whitespace

This commit is contained in:
Kyle Drake 2014-06-08 18:32:33 -07:00
parent e9b7826621
commit e0efe996b0
6 changed files with 107 additions and 46 deletions

1
.gitignore vendored
View file

@ -22,3 +22,4 @@ public/assets/css/.sass-cache/
public/site_thumbnails
public/sites
public/site_screenshots
*.swp

8
app.rb
View file

@ -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
)

View file

@ -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
@ -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)

View file

@ -91,6 +91,43 @@ describe 'signup' do
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

View file

@ -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

View file

@ -61,7 +61,7 @@
<hr>
<p>You can optionally enter some tags! Tags will allow others to find your site based on your interests, or your site's theme. <b>Separate multiple tags with commas</b>. 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).</p>
<p>You can optionally enter some tags! Tags will allow others to find your site based on your interests, or your site's theme. <b>Separate multiple tags with commas</b>. 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.</p>
<h5>Tags</h5>
<input class="input-Area" name="tags" type="text" style="width: 400px; max-width:100%" placeholder="pokemon, video games, bulbasaur" value="<%= params[:tags] %>" autocapitalize="off" autocorrect="off">