diff options
| author | Kir | 2011-10-31 21:43:34 +0400 |
|---|---|---|
| committer | Kir | 2011-10-31 21:43:34 +0400 |
| commit | 48c144ed34e2d5977bb8e5af8a9c7e8dfc253361 (patch) | |
| tree | a672937fb96b955f3c2d4364195926b3b14d01cb /app | |
| parent | 3ddd221f5f235e34afb9d4bbc7a4fc05228f73a4 (diff) | |
| download | inboxes-48c144ed34e2d5977bb8e5af8a9c7e8dfc253361.tar.bz2 | |
Small changed
Diffstat (limited to 'app')
| -rw-r--r-- | app/controllers/discussions_controller.rb | 42 | ||||
| -rw-r--r-- | app/controllers/messages_controller.rb | 18 | ||||
| -rw-r--r-- | app/models/discussion.rb | 26 | ||||
| -rw-r--r-- | app/models/discussion_view.rb | 7 | ||||
| -rw-r--r-- | app/models/message.rb | 10 | ||||
| -rw-r--r-- | app/views/discussions/_form.html.haml | 2 | ||||
| -rw-r--r-- | app/views/discussions/index.html.haml | 8 | ||||
| -rw-r--r-- | app/views/discussions/new.html.haml | 3 | ||||
| -rw-r--r-- | app/views/discussions/show.html.haml | 14 | ||||
| -rw-r--r-- | app/views/messages/_form.html.haml | 6 |
10 files changed, 86 insertions, 50 deletions
diff --git a/app/controllers/discussions_controller.rb b/app/controllers/discussions_controller.rb index 7d5d8ca..8ea6459 100644 --- a/app/controllers/discussions_controller.rb +++ b/app/controllers/discussions_controller.rb @@ -1,6 +1,8 @@ class DiscussionsController < ApplicationController - # load_and_authorize_resource - # before_filter :load_and_check_discussion_recipient, :only => [:create, :new] + before_filter :authenticate_user! + before_filter :check_permissions, :only => :show + + before_filter :load_and_check_discussion_recipient, :only => [:create, :new] def index # показываем дискуссии юзера, и те куда его присоеденили @@ -17,53 +19,46 @@ class DiscussionsController < ApplicationController # GET /discussions/1.json def show @discussion = Discussion.includes(:messages, :speakers).find(params[:id]) - # @discussion.mark_as_read_for(current_user) # сделаем прочтенной для пользователя + redirect_to root_url, :notice => t("views.discussions.can_not_participate") unless @discussion.can_participate?(current_user) - # члены дискуссии - приглашенные в нее + ее создатель - @message = Message.new - - respond_to do |format| - format.html # show.html.erb - format.json { render :json => @discussion } - end + @discussion.mark_as_read_for(current_user) # сделаем прочтенной для пользователя end # GET /discussions/new # GET /discussions/new.json def new - @discussion = Discussion.new + # @discussion = Discussion.new @discussion.messages.build end # POST /discussions # POST /discussions.json def create - @discussion = Discussion.new(params[:discussion]) + # @discussion = Discussion.new(params[:discussion]) + @discussion.add_recipient_token current_user.id + @discussion.messages.each do |m| m.discussion = @discussion m.user = current_user end - @discussion.add_recipient_token current_user.id - respond_to do |format| - if @discussion.save - format.html { redirect_to @discussion, :notice => 'Дискуссия начата.' } - # format.json { render :json => @discussion, :status => :created, :location => @discussion } - else - format.html { render :action => "new" } - # format.json { render :json => @discussion.errors, :status => :unprocessable_entity } - end + + if @discussion.save + redirect_to @discussion, :notice => t("views.discussions.started") + else + render :action => "new" end end def leave @discussion.remove_speaker(current_user) - redirect_to discussions_url, :notice => "Вы успешно покинули дискуссию." + redirect_to discussions_url, :notice => t("views.discussions.leaved") end private def load_and_check_discussion_recipient + @discussion = Discussion.new((params[:discussion] ? params[:discussion] : {})) @discussion.recipient_tokens = params[:recipients] if params[:recipients] # проверка, существует ли уже дискуссия с этим человеком @@ -76,9 +71,10 @@ class DiscussionsController < ApplicationController Message.create!(:discussion => discussion, :user => current_user, :body => m.body) if m.body end # перекидываем на нее - redirect_to discussion_url(discussion), :notice => "Переписка между вами уже существует." + redirect_to discussion_url(discussion), :notice => t("views.discussions.exists", :user => user[Inboxes::config.user_name]) end end end + end diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb new file mode 100644 index 0000000..a302237 --- /dev/null +++ b/app/controllers/messages_controller.rb @@ -0,0 +1,18 @@ +class MessagesController < ApplicationController + + def create + @discussion = Discussion.find(params[:discussion_id]) + redirect_to root_url, :notice => t("views.discussions.can_not_participate") unless @discussion.can_participate?(current_user) + + @message = Message.new(params[:message]) + @message.user = current_user + @message.discussion = @discussion + @message.save + + respond_to do |format| + format.html { redirect_to @message.discussion } + format.js + end + end + +end diff --git a/app/models/discussion.rb b/app/models/discussion.rb index 474ad00..7898140 100644 --- a/app/models/discussion.rb +++ b/app/models/discussion.rb @@ -66,15 +66,13 @@ class Discussion < ActiveRecord::Base # проверяет, есть ли уже беседа между пользователями # TODO вынести в отдельный метод а в этом возращать true/false, а то неправославно как-то def self.find_between_users(user, user2) - res = nil + dialog = nil discussions = self.joins(:speakers).includes(:users).where("speakers.user_id = ?", user.id) Rails.logger.info "Searching for ids: #{user.id}, #{user2.id}" discussions.each do |discussion| - - res = discussion if discussion.private? && ((discussion.users.first == user && discussion.users.last == user2) || (discussion.users.first == user2 && discussion.users.last == user)) - Rails.logger.info "Searching for ids: #{discussion.users.inspect}" if discussion.private? + dialog = discussion if discussion.private? && ((discussion.users.first == user && discussion.users.last == user2) || (discussion.users.first == user2 && discussion.users.last == user)) end - res + dialog end # приватная/групповая @@ -88,14 +86,14 @@ class Discussion < ActiveRecord::Base end # проверка, является ли дискуссия непрочитанной для пользователя - # def unread_for?(user) - # flag = self.views.find_by_user_id(user.id) - # if flag - # self.updated_at >= flag.updated_at - # else - # true - # end - # end + def unread_for?(user) + flag = self.views.find_by_user_id(user.id) + if flag + self.updated_at >= flag.updated_at + else + true + end + end # пометить как прочитанная def mark_as_read_for(user) @@ -112,7 +110,7 @@ class Discussion < ActiveRecord::Base def check_that_has_at_least_two_users Rails.logger.info self.recipient_ids - errors.add :recipient_tokens, "Укажите хотя бы одного получателя" #if !self.recipient_ids || self.recipient_ids.size < 2 + errors.add :recipient_tokens, "Укажите хотя бы одного получателя" if !self.recipient_ids || self.recipient_ids.size < 2 end end
\ No newline at end of file diff --git a/app/models/discussion_view.rb b/app/models/discussion_view.rb new file mode 100644 index 0000000..5714bb5 --- /dev/null +++ b/app/models/discussion_view.rb @@ -0,0 +1,7 @@ +class DiscussionView < ActiveRecord::Base + belongs_to :user + belongs_to :discussion + + validates :user, :discussion, :presence => true + validates_uniqueness_of :user_id, :scope => :discussion_id +end diff --git a/app/models/message.rb b/app/models/message.rb index a38dfb0..b7f9b12 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -7,16 +7,16 @@ class Message < ActiveRecord::Base validates :user, :discussion, :body, :presence => true - # after_save :touch_discussion_and_mark_as_read + after_save :touch_discussion_and_mark_as_read - # def visible_for? user - # self.created_at.to_i >= self.discussion.user_invited_at(user).to_i - # end + def visible_for? user + self.created_at.to_i >= self.discussion.user_invited_at(user).to_i + end private def touch_discussion_and_mark_as_read self.discussion.touch - # self.discussion.mark_as_read_for(self.user) + self.discussion.mark_as_read_for(self.user) end end diff --git a/app/views/discussions/_form.html.haml b/app/views/discussions/_form.html.haml index 52da4a6..eab0db6 100644 --- a/app/views/discussions/_form.html.haml +++ b/app/views/discussions/_form.html.haml @@ -1,7 +1,7 @@ = form_for @discussion do |f| .entry = f.label :recipient_tokens - = f.collection_select :recipient_tokens, User.all, :id, :name, :prompt => true, :html_options => { :multiple => true } + = select_tag "discussion[recipient_tokens]", options_from_collection_for_select(User.all, :id, Inboxes::config.user_name), :multiple => true, :size => "4" .entry = f.fields_for :messages do |j| = j.label :body diff --git a/app/views/discussions/index.html.haml b/app/views/discussions/index.html.haml index 669c01b..8f47198 100644 --- a/app/views/discussions/index.html.haml +++ b/app/views/discussions/index.html.haml @@ -1,5 +1,7 @@ -%h3 Discussions +%h3 Discussions list - @discussions.each do |discussion| - = discussion.id + .discussion + = link_to "Discussion with #{discussion.users.collect{|u| u[Inboxes::config.user_name]}.join(', ')}", discussion -= link_to "Create new", new_discussion_path
\ No newline at end of file +%p + = link_to "Create new", new_discussion_path
\ No newline at end of file diff --git a/app/views/discussions/new.html.haml b/app/views/discussions/new.html.haml index 3f68ec9..ac6665c 100644 --- a/app/views/discussions/new.html.haml +++ b/app/views/discussions/new.html.haml @@ -1,2 +1,3 @@ %h3 New discussion -= render "form"
\ No newline at end of file += render "form" += link_to "All discussions", discussions_path
\ No newline at end of file diff --git a/app/views/discussions/show.html.haml b/app/views/discussions/show.html.haml index 8c13728..903d8fd 100644 --- a/app/views/discussions/show.html.haml +++ b/app/views/discussions/show.html.haml @@ -1,5 +1,13 @@ -= @discussion.users.map { |u| u.name }.join(", ") -= debug @discussion.speakers +%h3 + Chat with + = @discussion.users.map { |u| u[Inboxes::config.user_name] }.join(", ") +%h3 Messages - @discussion.messages.each do |message| - = message.body
\ No newline at end of file + %p + = "[#{message.user[Inboxes::config.user_name]}]" + = message.body + = "@ #{l(message.created_at)}" + += render "messages/form" += link_to "All discussions", discussions_path
\ No newline at end of file diff --git a/app/views/messages/_form.html.haml b/app/views/messages/_form.html.haml new file mode 100644 index 0000000..78ccaa0 --- /dev/null +++ b/app/views/messages/_form.html.haml @@ -0,0 +1,6 @@ +.message_form + = form_for Message.new, :url => discussion_messages_path(@discussion.id), :method => :post do |f| + = f.text_area :body + = f.submit "Отправить" + += link_to "Выйти из переписки", leave_discussion_path(@discussion), :method => :post unless @discussion.private?
\ No newline at end of file |
