Skip to content

Commit

Permalink
Merge branch 'master' of github.com:fdv/publify
Browse files Browse the repository at this point in the history
  • Loading branch information
Frédéric de Villamil committed Aug 14, 2013
2 parents f1e571d + 7be482e commit 470c673
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 118 deletions.
13 changes: 13 additions & 0 deletions app/controllers/admin/base_controller.rb
Expand Up @@ -17,6 +17,14 @@ def insert_editor

private

def update_settings_with!(params)
Blog.transaction do
params[:setting].each { |k,v| this_blog.send("#{k.to_s}=", v) }
this_blog.save
flash[:notice] = _('config updated.')
end
end

def save_a(object, title)
if object.save
flash[:notice] = _("#{title.capitalize} was successfully saved.")
Expand All @@ -28,8 +36,13 @@ def save_a(object, title)

def destroy_a(klass_to_destroy)
@record = klass_to_destroy.find(params[:id])
if @record.respond_to?(:access_by?) && !@record.access_by?(current_user)
flash[:error] = _("Error, you are not allowed to perform this action")
return(redirect_to action: 'index')
end
return render('admin/shared/destroy') unless request.post?
@record.destroy
flash[:notice] = _("This #{controller_name.humanize} was deleted successfully")
redirect_to action: 'index'
end

Expand Down
13 changes: 1 addition & 12 deletions app/controllers/admin/content_controller.rb
Expand Up @@ -85,18 +85,7 @@ def update
end

def destroy
@record = Article.find(params[:id])

unless @record.access_by?(current_user)
flash[:error] = _("Error, you are not allowed to perform this action")
return(redirect_to :action => 'index')
end

return(render 'admin/shared/destroy') unless request.post?

@record.destroy
flash[:notice] = _("This article was deleted successfully")
redirect_to :action => 'index'
destroy_a(Article)
end

def autosave
Expand Down
2 changes: 0 additions & 2 deletions app/controllers/admin/profiles_controller.rb
@@ -1,6 +1,4 @@
class Admin::ProfilesController < Admin::BaseController
helper Admin::UsersHelper

def index
@user = current_user
@profiles = Profile.find(:all, :order => 'id')
Expand Down
73 changes: 21 additions & 52 deletions app/controllers/admin/seo_controller.rb
@@ -1,76 +1,45 @@
class Admin::SeoController < Admin::BaseController
cache_sweeper :blog_sweeper
before_filter :set_setting, only: [:index, :titles]

def index
load_settings
if File.exists? "#{::Rails.root.to_s}/public/robots.txt"
@setting.robots = ""
file = File.readlines("#{::Rails.root.to_s}/public/robots.txt")
file.each do |line|
@setting.robots << line
end
else
build_robots
end
@setting.robots = Robot.new.rules
end

def permalinks
if request.post?
if params[:setting]['permalink_format'] and params[:setting]['permalink_format'] == 'custom'
params[:setting]['permalink_format'] = params[:setting]['custom_permalink']
update_settings
else
set_setting
if @setting.permalink_format != '/%year%/%month%/%day%/%title%' and
@setting.permalink_format != '/%year%/%month%/%title%' and
@setting.permalink_format != '/%title%'
@setting.custom_permalink = @setting.permalink_format
@setting.permalink_format = 'custom'
end
update
return
end

load_settings
if @setting.permalink_format != '/%year%/%month%/%day%/%title%' and
@setting.permalink_format != '/%year%/%month%/%title%' and
@setting.permalink_format != '/%title%'
@setting.custom_permalink = @setting.permalink_format
@setting.permalink_format = 'custom'
end
end

def titles
load_settings
end

def update
if request.post?
Blog.transaction do
params[:setting].each { |k,v| this_blog.send("#{k.to_s}=", v) }
this_blog.save
flash[:notice] = _('config updated.')
end

save_robots unless params[:setting][:robots].blank?

redirect_to :action => params[:from]
end
update_settings if request.post?
rescue ActiveRecord::RecordInvalid
render params[:from]
end

private
def load_settings
@setting = this_blog
end

def save_robots
if File.writable? "#{::Rails.root.to_s}/public/robots.txt"
robots = File.new("#{::Rails.root.to_s}/public/robots.txt", "r+")
robots.write(params[:setting][:robots])
robots.close
def update_settings
if params[:setting]['permalink_format'] and params[:setting]['permalink_format'] == 'custom'
params[:setting]['permalink_format'] = params[:setting]['custom_permalink']
end
update_settings_with!(params)
if params[:setting][:robots].present?
Robot.new.add(params[:setting][:robots])
end
redirect_to action: params[:from]
end

def build_robots
robots = File.new("#{::Rails.root.to_s}/public/robots.txt", "w+")
line = "User-agent: *\nAllow: /\nDisallow: /admin\n"
robots.write(line)
robots.close
@setting.robots = line
def set_setting
@setting = this_blog
end

end
11 changes: 3 additions & 8 deletions app/controllers/admin/settings_controller.rb
Expand Up @@ -10,21 +10,16 @@ def index

def write; load_settings end
def feedback; load_settings end

def redirect
flash[:notice] = _("Please review and save the settings before continuing")
redirect_to :action => "index"
end

def update
if request.post?
Blog.transaction do
params[:setting].each { |k,v| this_blog.send("#{k.to_s}=", v) }
this_blog.save
flash[:notice] = _('config updated.')
end

redirect_to :action => params[:from]
update_settings_with!(params)
redirect_to action: params[:from]
end
rescue ActiveRecord::RecordInvalid
render params[:from]
Expand Down
37 changes: 13 additions & 24 deletions app/controllers/admin/statuses_controller.rb
Expand Up @@ -3,49 +3,38 @@ module Admin; end
class Admin::StatusesController < Admin::ContentController
layout "administration"

def index; redirect_to :action => 'new' ; end
def index; redirect_to :action => 'new' ; end
def new; new_or_edit; end
def edit; new_or_edit; end

def destroy
@record = Status.find(params[:id])

unless @record.access_by?(current_user)
flash[:error] = _("Error, you are not allowed to perform this action")
return(redirect_to :action => 'index')
end

return(render 'admin/shared/destroy') unless request.post?

@record.destroy
flash[:notice] = _("This status was deleted successfully")
redirect_to :action => 'index'
def destroy
destroy_a(Status)
end

private
def get_or_build_status
id = params[:id]
return Status.find(id) if id

Status.new do |status|
status.text_filter = current_user.default_text_filter
status.published = true
status.published_at = Time.now
status.push_to_twitter = true
end
end
end

def update_status_attributes
@status.attributes = params[:status]
@status.published_at = parse_date_time params[:status][:published_at]
@status.set_author(current_user)
@status.text_filter ||= current_user.default_text_filter
end

def new_or_edit
@statuses = Status.page(params[:page]).per(this_blog.limit_article_display)
@status = get_or_build_status

if request.post?
update_status_attributes

Expand All @@ -55,19 +44,19 @@ def new_or_edit
flash[:error] = _("Error, you are not allowed to perform this action")
return(redirect_to :action => 'new')
end

message = "updated"
end

if @status.save
@status.send_to_twitter(current_user) if params[:status][:push_to_twitter] and @status.twitter_id.nil? or @status.twitter_id.empty?
flash[:notice] = _("Status was successfully %s.", message)
redirect_to :action => 'new'
end
return
end

render 'new'
end

end
14 changes: 0 additions & 14 deletions app/helpers/admin/users_helper.rb

This file was deleted.

27 changes: 27 additions & 0 deletions app/models/robot.rb
@@ -0,0 +1,27 @@
class Robot

FILE = "#{::Rails.root.to_s}/public/robots.txt"
DEFAULT_LINE = "User-agent: *\nAllow: /\nDisallow: /admin\n"

attr_reader :robots

def initialize
unless File.exists?(FILE)
robots = File.new(FILE, "w+")
robots.write(DEFAULT_LINE)
robots.close
end
@robots = File.new(FILE, "r+")
end

def add(rules)
if File.writable?(FILE)
robots.write(rules)
robots.close
end
end

def rules
robots.readlines.join('')
end
end
14 changes: 11 additions & 3 deletions app/models/user.rb
Expand Up @@ -43,7 +43,7 @@ class User < ActiveRecord::Base
setting :twitter_oauth_token, :string, ''
setting :twitter_oauth_token_secret, :string, ''
setting :twitter_profile_image, :string, ''

# echo "publify" | sha1sum -
class_attribute :salt

Expand All @@ -58,6 +58,14 @@ def initialize(*args)
self.settings ||= {}
end

def first_and_last_name
return '' unless firstname.present? && lastname.present?
"#{firstname} #{lastname}"
end

def display_names
[:login, :nickname, :firstname, :lastname, :first_and_last_name].map{|f| send(f)}.delete_if{|e| e.empty?}
end

def self.authenticate(login, pass)
where("login = ? AND password = ? AND state = ?", login, password_hash(pass), 'active').first
Expand Down Expand Up @@ -181,7 +189,7 @@ def permalink
def admin?
profile.label == Profile::ADMIN
end

def update_twitter_profile_image(img)
return if self.twitter_profile_image == img
self.twitter_profile_image = img
Expand All @@ -200,7 +208,7 @@ def twitter_configured?
return false if self.twitter_oauth_token_secret.nil? or self.twitter_oauth_token_secret.empty?
true
end

protected

# Apply SHA1 encryption to the supplied password.
Expand Down
6 changes: 3 additions & 3 deletions app/views/admin/users/_form.html.erb
Expand Up @@ -70,7 +70,7 @@
<label class='control-label' for="user_login"><%= _("Display Name")%></label>
<div class='controls'>
<select name="user[name]">
<%= render_options_for_display_name %>
<%= options_for_select(@user.display_names, @user.name) %>
</select>
</div>
</div>
Expand Down Expand Up @@ -212,10 +212,10 @@
<div class='controls'>
<%= text_area('user', 'description', {:class => 'span5', :rows => 5}) %>
</div>
</div>
</div>
</fieldset>
<% end %>

<div class='form-actions'>
<%= cancel_or_save %>
</div>
</div>

0 comments on commit 470c673

Please sign in to comment.