Skip to content
This repository has been archived by the owner on Jun 7, 2021. It is now read-only.

Commit

Permalink
moving some assets around, fiing tag extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
bborn committed Feb 19, 2015
1 parent f3aabb1 commit 23d8a6f
Show file tree
Hide file tree
Showing 44 changed files with 359 additions and 611 deletions.
14 changes: 14 additions & 0 deletions app/assets/javascripts/active_admin.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,18 @@
#= require jquery.turbolinks
#= require turbolinks
#= require ckeditor/init
#= require tag-it/tag-it
#= require_directory ./ckeditor/rte

$(document).on 'page:receive', ->
$('input#tags').each ->
$.ajax
type: 'get'
url: $(this).data('auto_complete_url')
success: (data) ->
$(this).tagit
availableTags: data
allowSpaces: true
tagLimit: '20'
stopWritingOnTagLimit: true
return
4 changes: 4 additions & 0 deletions app/assets/stylesheets/active_admin.css.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


// SASS variable overrides must be declared before loading up Active Admin's styles.
//
// To view the variables that Active Admin provides, take a look at
Expand All @@ -10,6 +12,8 @@
// Active Admin's got SASS!
@import "active_admin/mixins";
@import "active_admin/base";
@import 'tag-it/tag-it';
@import 'tag-it/tagit.ui-zendesk';

// Overriding any non-variable SASS must be done after the fact.
// For example, to change the default status-tag color:
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
require 'pp'

class BaseController < ApplicationController

include AuthenticatedSystem
include Pundit
include LocalizedApplication
include BaseHelper

Expand Down
11 changes: 3 additions & 8 deletions app/controllers/sb_posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,11 @@ def destroy
end

protected
#overide in your app
def authorized?
%w(create new).include?(action_name) || @post.editable_by?(current_user)
end

def find_post
@post = SbPost.find_by_id_and_topic_id_and_forum_id(params[:id].to_i, params[:topic_id].to_i, params[:forum_id].to_i) || raise(ActiveRecord::RecordNotFound)
end

def sb_post_params
params[:sb_post].permit(:body, :author_email, :author_ip, :author_name, :author_url)
end
def sb_post_params
params[:sb_post].permit(:body, :author_email, :author_ip, :author_name, :author_url)
end
end
17 changes: 13 additions & 4 deletions app/controllers/topics_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class TopicsController < BaseController
before_action :find_forum_and_topic, :except => :index
before_action :login_required, :except => [:index, :show]
after_action :verify_authorized, :except => [:index, :show]


def index
@forum = Forum.find(params[:forum_id])
Expand All @@ -16,6 +18,7 @@ def index
def new
@topic = Topic.new
@topic.sb_posts.build
authorize @topic
end

def show
Expand Down Expand Up @@ -45,6 +48,8 @@ def show

def create
@topic = @forum.topics.new(topic_params)
authorize @topic

assign_protected

@post = @topic.sb_posts.first
Expand Down Expand Up @@ -72,8 +77,14 @@ def create
end
end

def edit
authorize @topic
end

def update
assign_protected

authorize @topic
@topic.tag_list = params[:tag_list] || ''
@topic.update_attributes!(topic_params)
respond_to do |format|
Expand All @@ -83,6 +94,8 @@ def update
end

def destroy
authorize @topic

@topic.destroy
flash[:notice] = :topic_deleted.l_with_args(:topic => CGI::escapeHTML(@topic.title))
respond_to do |format|
Expand Down Expand Up @@ -110,10 +123,6 @@ def find_forum_and_topic
@topic = @forum.topics.find(params[:id]) if params[:id]
end

#overide in your app
def authorized?
%w(new create).include?(action_name) || @topic.editable_by?(current_user)
end

def topic_params
params[:topic].permit(:tag_list, :title, :sticky, :locked, {:sb_posts_attributes => [:body]}, :forum_id)
Expand Down
119 changes: 57 additions & 62 deletions app/models/acts_as_taggable_on/tag.rb
Original file line number Diff line number Diff line change
@@ -1,72 +1,67 @@
#require_dependency ActsAsTaggableOn::Engine.config.root.join('app', 'models', 'acts_as_taggable_on', 'tag.rb').to_s
# require_dependency ActsAsTaggableOn::Engine.config.root.join('lib', 'acts_as_taggable_on', 'tag.rb').to_s

class ActsAsTaggableOn::Tag < ActiveRecord::Base
# ActsAsTaggableOn::Tag.instance_eval do
# def popular(limit = 20, type = nil)
# tags = ActsAsTaggableOn::Tag.counts(:at_least => 0).limit(limit).order('count DESC')
# tags = tags.where("taggings.taggable_type = ?", type.capitalize) if type
# tags
# end

class << self
def popular(limit = 20, type = nil)
tags = ActsAsTaggableOn::Tag.counts(:at_least => 0).limit(limit).order('count DESC')
tags = tags.where("taggings.taggable_type = ?", type.capitalize) if type
tags
end
# def default_per_page
# 25
# end

def default_per_page
25
end
# # Calculate the tag counts for all tags.
# #
# # - +:start_at+ - restrict the tags to those created after a certain time
# # - +:end_at+ - restrict the tags to those created before a certain time
# # - +:at_least+ - exclude tags with a frequency less than the given value
# # - +:at_most+ - exclude tags with a frequency greater than the given value
# #
# # Deprecated:
# #
# # - +:conditions+
# # - +:limit+
# # - +:order+
# #
# def counts(options = {})
# options.assert_valid_keys :start_at, :end_at, :at_least, :at_most, :conditions, :limit, :order, :joins

# Calculate the tag counts for all tags.
#
# - +:start_at+ - restrict the tags to those created after a certain time
# - +:end_at+ - restrict the tags to those created before a certain time
# - +:at_least+ - exclude tags with a frequency less than the given value
# - +:at_most+ - exclude tags with a frequency greater than the given value
#
# Deprecated:
#
# - +:conditions+
# - +:limit+
# - +:order+
#
def counts(options = {})
options.assert_valid_keys :start_at, :end_at, :at_least, :at_most, :conditions, :limit, :order, :joins
# tags = select("#{quoted_table_name}.id", "#{quoted_table_name}.name", "COUNT(#{quoted_table_name}.id) AS count")
# tags = tags.joins(:taggings)
# tags = tags.having(["COUNT(#{ActsAsTaggableOn::Tagging.quoted_table_name}.id) >= ?", options[:at_least]]) if options[:at_least]
# tags = tags.having(["COUNT(#{ActsAsTaggableOn::Tagging.quoted_table_name}.id) <= ?", options[:at_most]]) if options[:at_most]
# tags = tags.where("#{ActsAsTaggableOn::Tagging.quoted_table_name}.created_at >= ?", options[:start_at]) if options[:start_at]
# tags = tags.where("#{ActsAsTaggableOn::Tagging.quoted_table_name}.created_at <= ?", options[:end_at]) if options[:end_at]
# tags = tags.where(options[:conditions]) if options[:conditions]
# tags = tags.limit(options[:limit]) if options[:limit]
# tags = tags.order(options[:order]) if options[:order]

tags = select("#{quoted_table_name}.id", "#{quoted_table_name}.name", "COUNT(#{quoted_table_name}.id) AS count")
tags = tags.joins(:taggings)
tags = tags.having(["COUNT(#{ActsAsTaggableOn::Tagging.quoted_table_name}.id) >= ?", options[:at_least]]) if options[:at_least]
tags = tags.having(["COUNT(#{ActsAsTaggableOn::Tagging.quoted_table_name}.id) <= ?", options[:at_most]]) if options[:at_most]
tags = tags.where("#{ActsAsTaggableOn::Tagging.quoted_table_name}.created_at >= ?", options[:start_at]) if options[:start_at]
tags = tags.where("#{ActsAsTaggableOn::Tagging.quoted_table_name}.created_at <= ?", options[:end_at]) if options[:end_at]
tags = tags.where(options[:conditions]) if options[:conditions]
tags = tags.limit(options[:limit]) if options[:limit]
tags = tags.order(options[:order]) if options[:order]
# if joins = options.delete(:joins)
# tags = tags.joins(joins)
# end

if joins = options.delete(:joins)
tags = tags.joins(joins)
end
# tags.group("#{quoted_table_name}.id, #{quoted_table_name}.name")
# end
# end

tags.group("#{quoted_table_name}.id, #{quoted_table_name}.name")
end
# ActsAsTaggableOn::Tag.class_eval do
# def to_param
# URI.escape(URI.escape(self.name), /[\/.?#]/)
# end

# def related_tags(limit = 10)
# taggables = self.taggings.limit(10).to_a.collect{|t| t.taggable }.compact

end

def to_param
URI.escape(URI.escape(self.name), /[\/.?#]/)
end

def related_tags(limit = 10)
taggables = self.taggings.limit(10).to_a.collect{|t| t.taggable }.compact

tagging_ids = taggables.map{|t| t.taggings.limit(10).map(&:id) }.flatten.uniq
return [] if tagging_ids.blank?

ActsAsTaggableOn::Tag.where("tags.id != '#{self.id}'")
.select("tags.id, tags.name, COUNT(tags.id) as count")
.joins(:taggings)
.where({:taggings => {:id => tagging_ids }})
.group("tags.id, tags.name")
.order("count DESC")
.limit(limit)
end

end
# tagging_ids = taggables.map{|t| t.taggings.limit(10).map(&:id) }.flatten.uniq
# return [] if tagging_ids.blank?

# ActsAsTaggableOn::Tag.where("tags.id != '#{self.id}'")
# .select("tags.id, tags.name, COUNT(tags.id) as count")
# .joins(:taggings)
# .where({:taggings => {:id => tagging_ids }})
# .group("tags.id, tags.name")
# .order("count DESC")
# .limit(limit)
# end
# end
11 changes: 11 additions & 0 deletions app/policies/active_admin/page_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class ActiveAdmin::PagePolicy < ApplicationPolicy

def dashboard?
true
end

def index?
true
end

end
59 changes: 59 additions & 0 deletions app/policies/application_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
class ApplicationPolicy
attr_reader :user, :record

def initialize(user, record)
@user = user
@record = record
end

def show?
true
end

def index?
true
end

def new?
create?
end

def create?
true
end

def edit?
update?
end

def update?
user.admin? || record.respond_to(:user) && record.user.eql?(user)
end

def destroy?
update?
end

def destroy_all?
user.admin?
end

def scope
Pundit.policy_scope!(user, record.class)
end

class Scope
attr_reader :user, :scope

def initialize(user, scope)
@user = user
@scope = scope
end

def resolve
scope
end
end

end

15 changes: 15 additions & 0 deletions app/policies/sb_post_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class SbPostPolicy < ApplicationPolicy

def edit?
update?
end

def destroy?
update?
end

def update?
record.editable_by?(user)
end

end
23 changes: 23 additions & 0 deletions app/policies/topic_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class TopicPolicy < ApplicationPolicy

def new?
create?
end

def create?
true
end

def edit?
update?
end

def destroy?
update?
end

def update?
record.editable_by?(user)
end

end
4 changes: 4 additions & 0 deletions app/views/posts/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@

-content_for :end_javascript do
:javascript
$.ajax({
type: "PUT",
url: "#{update_views_user_post_path(@post.user, @post)}",
});
$('.email').on('click', function(e){
e.preventDefault();
$.post("#{send_to_friend_user_post_path(:user_id => @user.id, :id => @post.id)}", function(response){
Expand Down
Loading

0 comments on commit 23d8a6f

Please sign in to comment.