This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Array | |
def all_but?(number = 0) | |
count = 0 | |
if !block_given? | |
each { |item| count += 1 if item } | |
else | |
each { |item| count += 1 if yield(item) } | |
end | |
count >= length - number | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SitesHelper | |
def postable? | |
[email protected] || (@site.locked && session[@site.name.to_sym] == "session-unlocked") | |
end | |
def private? | |
@site.locked && session[@site.name.to_sym] != "session-unlocked" | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SitesHelper | |
def private? | |
@site.locked && session[@site.name.to_sym] != "session-unlocked" | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def find_site | |
@slug = params[:id].downcase | |
@site = Site.find_by(name: @slug) | |
if @site.present? | |
expired? | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def home | |
@slug = ('a'..'z').to_a.shuffle[0,8].join | |
redirect_to main_path(@slug) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SitesController < ApplicationController | |
... | |
private | |
def find_site | |
@slug = params[:id].downcase | |
@site = Site.find_by(name: @slug) | |
if @site.present? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostsController < ApplicationController | |
before_action :editable?, only: [:create] | |
def create | |
@site = Site.find_by(name: params[:post][:site]) | |
@post = @site.posts.build(post_params) | |
if @post.save | |
redirect_to main_path(@post.site.name) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# sites_controller.rb | |
def unlocked? | |
if private? | |
redirect_to main_path(@site.name), notice: 'Site is locked.' | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SessionsController < ApplicationController | |
def create | |
site = Site.find_by(name: params[:session][:site]) | |
if site && site.authenticate(params[:session][:password]) | |
session[site.name.to_sym] = "session-unlocked" | |
redirect_to main_path(site.name), notice: 'Logged in. Free to add/delete posts' | |
else | |
flash.now[:danger] = 'Invalid password' | |
redirect_to main_path(site.name), notice: 'Incorrect password' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= form_for(:session, url: unlock_path) do |f| %> | |
<%= f.label :password %> | |
<%= f.password_field :password %> | |
<%= f.hidden_field :site, value: @site.name %> | |
<%= f.submit "Unlock" %> | |
<% end %> |
NewerOlder