forked from mendicant-original/community
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add activity models, controllers, and index
- Loading branch information
Jordan Byron
committed
Jan 18, 2012
1 parent
9d645ec
commit cc6db4c
Showing
21 changed files
with
263 additions
and
52 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
div.activity | ||
+container | ||
div.activity_title | ||
+column(20) | ||
font-size: 1.5em | ||
font-family: $display-font | ||
margin-bottom: 1em | ||
line-height: 0.9 | ||
span.registration | ||
float: right | ||
color: #666 | ||
a | ||
font-size: 0.75em | ||
color: #333 | ||
&.participating:before | ||
content: "" | ||
float: left | ||
width: 26px | ||
height: 1em | ||
background: asset-url("icons/check.png", image) no-repeat | ||
&:hover | ||
color: #333 | ||
a | ||
color: $link-active-color | ||
&:hover | ||
color: $link-color | ||
|
||
div.activity_content | ||
+column(15) | ||
h1, h2, h3, h4 | ||
font-size: 1em | ||
div.activity_info | ||
+column(5, true) | ||
text-align: right | ||
div.control-bar | ||
+column(20) | ||
margin-bottom: 0 |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class ActivitiesController < ApplicationController | ||
before_filter :user_required | ||
before_filter :find_activity, :only => [:show, :edit, :update, :destroy, :register] | ||
|
||
def index | ||
@activities = Activity.includes(:author).order("created_at desc"). | ||
paginate(:page => params[:page]) | ||
@activities = ActivityDecorator.decorate(@activities) | ||
end | ||
|
||
def register | ||
registration = @activity.activity_registrations. | ||
where(:user_id => current_user.id) | ||
|
||
if registration.any? | ||
registration.first.destroy | ||
else | ||
registration.create | ||
end | ||
|
||
@activity = ActivityDecorator.decorate(@activity) | ||
|
||
respond_to do |format| | ||
format.js | ||
end | ||
end | ||
|
||
private | ||
|
||
def find_activity | ||
@activity = Activity.find_by_slug(params[:id]) | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
class ActivityDecorator < ApplicationDecorator | ||
decorates :activity | ||
|
||
def registration_button | ||
h.content_tag(:span, :class => "registration") do | ||
if activity.registration_open? | ||
if activity.approved_participants.include?(h.current_user) | ||
text = "Participating" | ||
css_class = "participating" | ||
elsif activity.users.include?(h.current_user) | ||
text = "Pending Approval" | ||
else | ||
text = "Participate" | ||
end | ||
|
||
h.link_to(text, h.register_activity_path(activity), | ||
:class => "clean-gray #{css_class if css_class}", | ||
:method => :post, :remote => true) | ||
else | ||
"Registration Closed" | ||
end | ||
end | ||
end | ||
|
||
def created_at | ||
h.l activity.created_at.to_date, :format => :long | ||
end | ||
|
||
def author_link | ||
"by #{h.link_to(activity.author.name, h.person_path(activity.author))}".html_safe | ||
end | ||
|
||
def participants_link | ||
participants = activity.approved_participants | ||
|
||
if participants.any? | ||
link = h.link_to h.pluralize(participants.count, 'participant'), | ||
h.activity_path(activity, :anchor => "participants") | ||
end | ||
|
||
h.content_tag(:div, link || "", :class => "participants").html_safe | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,12 @@ | ||
class ApplicationDecorator < Draper::Base | ||
# Lazy Helpers | ||
# PRO: Call Rails helpers without the h. proxy | ||
# ex: number_to_currency(model.price) | ||
# CON: Add a bazillion methods into your decorator's namespace | ||
# and probably sacrifice performance/memory | ||
# | ||
# Enable them by uncommenting this line: | ||
# lazy_helpers | ||
|
||
# Shared Decorations | ||
# Consider defining shared methods common to all your models. | ||
# | ||
# Example: standardize the formatting of timestamps | ||
# | ||
# def formatted_timestamp(time) | ||
# h.content_tag :span, time.strftime("%a %m/%d/%y"), | ||
# :class => 'timestamp' | ||
# end | ||
# | ||
# def created_at | ||
# formatted_timestamp(model.created_at) | ||
# end | ||
# | ||
# def updated_at | ||
# formatted_timestamp(model.updated_at) | ||
# end | ||
def bottom(collection, collection_path) | ||
if collection | ||
h.tag(:hr, :class => "separator") if model != collection.last | ||
else | ||
h.link_to "« There is more where that came from".html_safe, | ||
collection_path, :id => "back-link" | ||
end | ||
end | ||
|
||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Activity < ActiveRecord::Base | ||
|
||
belongs_to :author, :class_name => "User" | ||
has_many :activity_registrations | ||
has_many :users, :through => :activity_registrations | ||
|
||
has_slug 'title', :max_length => 40, | ||
:on_conflict => :append_id | ||
|
||
validates_presence_of :title, :body, :author | ||
|
||
def to_param | ||
slug | ||
end | ||
|
||
def approved_participants | ||
users.includes(:activity_registrations). | ||
where("activity_registrations.approved = true") | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class ActivityRegistration < ActiveRecord::Base | ||
before_create :set_approval | ||
|
||
belongs_to :user | ||
belongs_to :activity | ||
|
||
attr_protected :approved | ||
|
||
private | ||
|
||
def set_approval | ||
self.approved = !activity.participation_moderated? | ||
|
||
return true | ||
end | ||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
= content_tag_for :div, activity do | ||
.activity_title | ||
- if @activities | ||
= link_to activity.title, activity_path(activity) | ||
- else | ||
= activity.title | ||
|
||
= activity.registration_button | ||
|
||
.activity_content= activity.body | ||
|
||
.activity_info | ||
= activity.participants_link | ||
= activity.author_link | ||
%br | ||
= activity.created_at | ||
|
||
- if false #activity.editable_by?(current_user) | ||
.control-bar | ||
= link_to 'Edit', edit_activity_path(activity), :class => "clean-gray" | ||
= link_to 'Destroy', activity, :confirm => 'Are you sure?', | ||
:method => :delete, :class => "clean-gray warn" | ||
|
||
= activity.bottom(@activities, activities_path) | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#activities= render :partial => "activity", :collection => @activities | ||
|
||
= will_paginate @activities |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
var button = '<%= escape_javascript @activity.registration_button %>'; | ||
var participants = '<%= escape_javascript @activity.participants_link %>'; | ||
|
||
$('#activity_<%= @activity.id %> span.registration').replaceWith(button); | ||
$('#activity_<%= @activity.id %> div.participants').replaceWith(participants); |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class CreateActivities < ActiveRecord::Migration | ||
def change | ||
create_table :activities do |t| | ||
t.integer :author_id | ||
t.string :title | ||
t.string :slug | ||
t.text :body | ||
|
||
t.boolean :registration_open, :null => false | ||
t.boolean :participation_moderated, :null => false | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
12 changes: 12 additions & 0 deletions
12
db/migrate/20120113162402_create_activity_registrations.rb
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class CreateActivityRegistrations < ActiveRecord::Migration | ||
def change | ||
create_table :activity_registrations do |t| | ||
t.belongs_to :user | ||
t.belongs_to :activity | ||
|
||
t.boolean :approved, :null => false | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains 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