Skip to content

Commit

Permalink
controller/views and new routes for managing shared_objects (model in…
Browse files Browse the repository at this point in the history
… acts_as_shareable)
  • Loading branch information
Boon Low committed May 14, 2010
1 parent 356728a commit fe614b5
Show file tree
Hide file tree
Showing 11 changed files with 416 additions and 0 deletions.
183 changes: 183 additions & 0 deletions app/controllers/member/shared_objects_controller.rb
@@ -0,0 +1,183 @@
class Member::SharedObjectsController < Member::BaseController

def index
@order = params[:order] || 'created_at'
@page = params[:page] || '1'
@asc = params[:asc] || 'desc'
@shared_objects = SharedObject.paginate :per_page => Tog::Config["plugins.tog_social.profile.list.page.size"],
:page => @page,
:order => "shared_objects.#{@order} #{@asc}"

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @shared_objects }
end
end

def show
@shared_object = SharedObject.find(params[:id])
@sharings = Share.find :all, :conditions => ["user_id = ? and shareable_id = ?", @shared_object.user.id, @shared_object.id]
store_location
respond_to do |format|
format.html
end
end

def new
@shareable = SharedObject.new
@shared_to = []
end

def create
if params[:commit] == 'Cancel'
redirect_back_or_default('/')
else
@shareable = SharedObject.new params[:shared_object]
@shareable.user = current_user
@shareable.type = 'SharedObject' # May be subclasses of shared object later such as 'link', 'comment' later
@shareable.published_at = '' if params[:shared_object][:state]=='draft' and params[:update_published_at]
@shareable.save

# Preparing to link the shared object to user commons (groups)
@sharing = Share.new
sharing_options = {}
sharing_options.merge!({:published_at=>@sharing.published_at}) if params[:update_published_at] # not in UI at the moment

# The group to share this with
shared_to = Group.find params[:group_id]

# User can also share the object simultaneous to other subscribed commons (groups)
if params[:other_group]
# Additional groups to share this with as specified by the users, including the current group
@groups = Group.find params[:other_group].collect! {|x| x.to_i } << shared_to
@groups.each do |a_group|
a_group.share(current_user, @shareable.type, @shareable.id, sharing_options)
end
user_message = @groups.collect { |x| current_user.groups.first == x ? current_user.profile.full_name : x.name }
flash[:ok] = "Object shared with " + user_message.join(', ')
else
# Share to the current group
shared_to.share(current_user, @shareable.type, @shareable.id, sharing_options)
user_message = current_user.groups.first == shared_to ? current_user.profile.full_name : shared_to.name + ' group'
flash[:ok] = "Object shared with " + user_message
end

#Save a copy of the shareable to user's own profile
current_user.groups.first.share(current_user, @shareable.type, @shareable.id, sharing_options) unless (current_user.id == shared_to.user_id && current_user.groups.first == shared_to)
redirect_back_or_default('/')
end
end

def edit
@shareable = SharedObject.find(params[:id])
# retrieve groups this shareble shared with
@shared_to = Share.find(:all, :conditions => ['shareable_id = ? AND user_id = ?', @shareable.id, @shareable.user.id]).collect {|share| share.shared_to_id }
end

def update
if params[:commit] == 'Cancel'
redirect_back_or_default('/')
else
@shareable = SharedObject.find params[:id]
if @shareable.user == current_user
@shareable.update_attributes(params[:shared_object]) if params[:shared_object]
new_shared_to = params[:other_group] || []
shares_in_other_groups = Share.find(:all, :conditions => ['shareable_id = ? AND user_id = ?', @shareable.id, @shareable.user.id]).delete_if { |share| share.shared_to_id == @shareable.user.groups.first.id }
shared_to = shares_in_other_groups.collect {|share| share.shared_to_id }
user_groups = @shareable.user.groups[1..-1]
sharing_options = {}
for group in user_groups
if new_shared_to.include?(group.id.to_s)
# share to this group if not already shared
group.share(current_user, @shareable.type.to_s, @shareable.id, sharing_options) unless shared_to.include?(group.id)
else
# remove share if already shared
Share.find(:first, :conditions => ['shareable_id = ? AND user_id = ? AND shared_to_id = ?', @shareable.id, @shareable.user.id, group.id]).destroy if shared_to.include?(group.id)
end
end
end
#need fixing
redirect_back_or_default('/')
end
end

def delete
@shareable = SharedObject.find params[:id]
@shared_to = Group.find params[:group_id]
sharing_query = params[:mode]=='all' ? [ 'shareable_id = ?', @shareable.id] : [ 'shareable_id = ? AND shared_to_id = ?', @shareable.id, @shared_to.id]
@related_sharings = Share.find(:all, :conditions => sharing_query)
unless @shareable.user == current_user || @shared_to.user_id == current_user.id
flash[:error] = "You are not permitted to delete this object."
redirect_back_or_default('/')
end
end

def destroy
if params[:commit] == 'Cancel'
redirect_back_or_default('/')
else
shareable = SharedObject.find params[:id]
shared_to = Group.find params[:group_id]
case params[:mode]
when 'all'
if shareable.user == current_user
sharing_ids = Share.find_all_by_shareable_id(shareable.id).collect(&:id)
Share.destroy sharing_ids
shareable.destroy
flash[:ok] = "The post and all the associated sharings are now deleted."
end
when 'sharing'
if shared_to.user_id == current_user.id
sharing_id = Share.find(:all, :conditions => ['shareable_id = ? AND shared_to_id = ?', shareable.id, shared_to.id]).collect(&:id)
Share.destroy sharing_id
flash[:ok] = "The post has been removed from " + shared_to.name + "."
end
else
flash[:notice] = I18n.t("tog_social.sharings.member.remove_share_nok")
end
respond_to do |format|
format.html { redirect_back_or_default('/') }
end
end

end

def publish
shareable = SharedObject.find params[:id]
if shareable.user == current_user
shareable.update_attribute(:state, "shared")
flash[:ok] = "The post has been published and visible publically."
else
flash[:notice] = "Unable to publish the post."
end
respond_to do |format|
format.html { redirect_back_or_default('/') }
end
end

def approve
sharing = Share.find params[:id]
if current_user.id == Group.find(sharing.shared_to_id).user_id
sharing.publish!
flash[:ok] = "You have approved this post - it's now visible publically."
else
flash[:notice] = "Unable to approve post."
end
respond_to do |format|
format.html { redirect_back_or_default('/') }
end
end

def share
if params[:commit] == 'Cancel'
redirect_back_or_default('/')
else
@shareable = SharedObject.find(params[:id])
# retrieve groups this shareble shared with
@shared_to = Share.find(:all, :conditions => ['shareable_id = ? AND user_id = ?', @shareable.id, @shareable.user.id]).collect {|share| share.shared_to_id }
# @all_groups = Group.active.collect { |group| group.type.to_s=='Homepage' ? nil : group }
# @all_groups.compact!
end
end

end
13 changes: 13 additions & 0 deletions app/controllers/shared_objects_controller.rb
@@ -0,0 +1,13 @@
class SharedObjectsController < ApplicationController

def index
end

def show
@shared_object = SharedObject.find(params[:id])
store_location
respond_to do |format|
format.html
end
end
end
10 changes: 10 additions & 0 deletions app/views/member/shared_objects/_common_form.html.erb
@@ -0,0 +1,10 @@
<p><label for="post_title">Title</label><br/><%= f.text_field :title, :class=>"wide" %></p>
<label for="post_body">Content - post (message, blog), link etc.</label><br/><%= f.text_area :content, :class=>"fieldbox wide" %>
<p><label for="post_tag_list">Tags, categories (separated by ,)</label><br/><%= f.text_field :tag_list, :class=>"fieldbox wide" %></p>

<p><label for="post_state">Status</label>
<%= f.select "state", options_for_select([[ "Share this now!", "shared" ], ["Draft - not visible publically", "draft" ]], @shareable.state) -%>
<!--label for="update_published_at"><%= check_box_tag "update_published_at", "1" %> "Publish this at"</label-->
<!--%= f.datetime_select :published_at-%-->
</p>

53 changes: 53 additions & 0 deletions app/views/member/shared_objects/delete.html.erb
@@ -0,0 +1,53 @@
<div class="detail detail1">
<h2>Delete</h2>

<% if @related_sharings.size > 1 %>
<p>You are about to delete the post shown below. Are you sure? This post has been shared with:</p>
<ul>
<% for related_sharing in @related_sharings
related_sharing_group = related_sharing.shared_to %>
<li><%= title_link_for_group_object(related_sharing_group) %></li>
<% end %>
</ul>
<% form_for :shared_object, @shared_object, :url => member_destroy_shared_object_path, :html => {:method => :delete} do |f| -%>
<div style="margin:0 0 0 20px;">
<%= submit_tag 'Delete All', :class => 'button mainaction', :style=>"background-color:#e40600; font-weight:bold; font-size:110%; padding:0.5em 1em; border: none;" %>
<%= submit_tag 'Cancel', :class => 'button mainaction', :style=>"font-weight:bold; font-size:110%; padding:0.5em 1em; border: none;" %>
Or <%= link_to 'Edit Sharing', member_share_existing_shared_object_path %>
</div>
<% end -%>
<% else %>
<%if params[:mode] =='sharing'%>
<p>You are about to remove the following post from
<%= link_to @shared_to.name, @shared_to %> (the original post still exists unless
<%= link_to @shareable.user.profile.full_name, profile_path(@shareable.user.profile) %> choose to delete it). Are you sure?</p>
<% form_for :shared_object, @shared_object, :url => member_destroy_shared_object_path, :html => {:method => :delete} do |f| -%>
<div style="margin:0 0 0 20px;">
<%= submit_tag 'Remove from Group', :class => 'button mainaction', :style=>"background-color:#e40600; font-weight:bold; font-size:110%; padding:0.5em 1em; border: none;" %>
<%= submit_tag 'Cancel', :class => 'button mainaction', :style=>"font-weight:bold; font-size:110%; padding:0.5em 1em; border: none;" %>
</div>
<% end -%>
<% else %>
<p>You are about to delete the post shown below. Are you sure?</p>
<% form_for :shared_object, @shared_object, :url => member_destroy_shared_object_path, :html => {:method => :delete} do |f| -%>
<div style="margin:0 0 0 20px;">
<%= submit_tag 'Delete', :class => 'button mainaction', :style=>"background-color:#e40600; font-weight:bold; font-size:110%; padding:0.5em 1em; border: none;" %>
<%= submit_tag 'Cancel', :class => 'button mainaction', :style=>"font-weight:bold; font-size:110%; padding:0.5em 1em; border: none;" %>
</div>
<% end -%>
<% end %>
<% end %>

<div style="background-color:#ddd; padding: 1em 1em 0 1em; margin: 2em 0 0 0;">
<p>
Submitted by <%= link_to @shareable.user.profile.full_name, profile_path(@shareable.user.profile) %>,
about <%= time_ago_in_words(@shareable.created_at) %> ago - <%= I18n.l(@shareable.created_at, :format => :short) %>.
</p>
</div>
<div style="background-color:#eee; padding: 1em;">
<% if !@shareable.title.blank? %>
<h3><%= link_to @shareable.title, shared_objects_path + '/' + @shareable.id.to_s %></h3>
<% end %>
<%= sanitize(textilize(@shareable.content[0..500]), :tags => (Tog::Plugins.settings :tog_core, 'sanitized.comments.allowed_tags').split, :attributes => (Tog::Plugins.settings :tog_core, 'sanitized.comments.allowed_attributes').split) %> <%= link_to("More..", shared_objects_path + '/' + @shareable.id.to_s) unless @shareable.content.size < 500 %>
</div>
</div>
13 changes: 13 additions & 0 deletions app/views/member/shared_objects/edit.html.erb
@@ -0,0 +1,13 @@
<div class="titleB">Edit Post</div>

<br/>
<%= error_messages_for :shared_object %>
<% form_for(:shared_object, @shareable, :url=> member_update_shared_object_path(@shareable.id),:html => { :method => :put }) do |f| %>
<fieldset>
<%= render :partial => 'common_form', :locals => {:f => f}%>
</fieldset>
<div style="margin:20px 0 0 0">
<%= submit_tag 'Save This', :class => 'button mainaction' %> <%= submit_tag 'Cancel', :class => 'button mainaction' %>
</div>
<% end %>
Empty file.
29 changes: 29 additions & 0 deletions app/views/member/shared_objects/new.html.erb
@@ -0,0 +1,29 @@
<div class="titleB">Create a New Post</div>

<br/>
<%= error_messages_for :shared_object %>
<% form_for :shared_object, @shared_object, :url => member_create_shared_object_path do |f| -%>
<fieldset>
<%= render :partial => 'common_form', :locals => {:f => f}%>
<% if current_user.groups.size > 1 %>
Also share this with the following groups which I've subscribed to:
<p style="background-color:#eee; padding: 10px 0 10px 10px; }">
<% for group in current_user.groups[1..-1]
if params[:group_id]==group.id.to_s %>
<%= check_box_tag '', group.id, true, :disabled => true %>
<%= group.name %> (posting to this group)
<br/>
<% else %>
<%= check_box_tag 'other_group[]', group.id, @shared_to.include?(group.id) %>
<%= group.name %>
<br/>
<% end %>
<% end %>
</p>
<% end %>
</fieldset>
<div style="margin:20px 0 0 0">
<%= submit_tag 'Create This', :class => 'button mainaction' %> <%= submit_tag 'Cancel', :class => 'button mainaction' %>
</div>
<% end %>
26 changes: 26 additions & 0 deletions app/views/member/shared_objects/share.html.erb
@@ -0,0 +1,26 @@
<div class="titleB">Share the Post with</div>

<br/>
<%= error_messages_for :shared_object %>
<% if current_user.groups.size > 1 %>
<% form_for :shared_object, @shared_object, :url=> member_update_shared_object_path(@shareable.id),:html => { :method => :put } do |f| -%>
<fieldset>
<h3>My subscribed groups <span style="font-weight:normal; font-size:88%;"> - unchecked to remove share</span></h3>
<p style="background-color:#eee; padding: 10px 0 10px 10px; }">
<% for group in current_user.groups[1..-1] %>
<%= check_box_tag 'other_group[]', group.id, @shared_to.include?(group.id) %>
<%= group.name %>
<br/>
<% end %>
</p>
</fieldset>
<div style="margin:20px 0 0 0">
<%= submit_tag 'Update This', :class => 'button mainaction' %> <%= submit_tag 'Cancel', :class => 'button mainaction' %>
</div>
<% end %>
<% else %>
<p>
<%= link_to 'Create or join some groups to share objects with.', '/groups' %>
</p>
<% end %>
42 changes: 42 additions & 0 deletions app/views/shared/_shared_object_properties_actions.html.erb
@@ -0,0 +1,42 @@
<p>
From <%= link_to sharing.shareable.user.profile.full_name, '/'+sharing.shareable.user.login %>,
about <%= time_ago_in_words(sharing.created_at) %> ago - <%= I18n.l(sharing.created_at, :format => :short) %>.

<% if sharing.shareable.state == "draft" && current_user == sharing.shareable.user %>
<strong>Draft version</strong> only you can see this -
<%= link_to "Make public", member_publish_shared_object_path(sharing.shareable.id) %> |
<% end %>
<% if current_user == sharing.shareable.user %>
<%= link_to "Edit", edit_member_shared_object_path(sharing.shareable.id) %> |
<% end %>
<% if current_user == sharing.shareable.user %>
<%= link_to "Delete", member_delete_shared_object_path(sharing.shared_to.id,sharing.shareable.id,:all), :method => :post %>
<% elsif current_user == sharing.shared_to.author || sharing.shared_to.moderators.include?(current_user) %>
<%= link_to "Delete", member_delete_shared_object_path(sharing.shared_to.id,sharing.shareable.id,:sharing), :method => :post %>
<% end %>
<% other_groups = related_shared_to(sharing, sharing.shared_to) %>
<% if other_groups.size!=0 %>
<br/>
Also shared in:
<% for other_group in other_groups %>
<%= group_or_homepage_link(other_group) %>
<% end %>
<%= link_to 'Edit sharings', member_share_existing_shared_object_path(sharing.shareable.id) if current_user == sharing.shareable.user %>
<% else %>
<% if current_user == sharing.shareable.user %>
<br/>
<%= link_to 'Share this with other groups', member_share_existing_shared_object_path(sharing.shareable.id) %>
<% end %>
<% end %>
<% if sharing.state == 'pending' %>
<br/>
<strong>Pending</strong> awaiting approval (not viewable publically).
<% if current_user == profile.user %>
<%= link_to 'Approve this', member_approve_sharing_path(sharing.id) %>
<% end %>
<% end %>
</p>

0 comments on commit fe614b5

Please sign in to comment.