diff options
| author | Kir | 2011-11-01 14:18:21 +0400 |
|---|---|---|
| committer | Kir | 2011-11-01 14:18:21 +0400 |
| commit | 49e19f446e625ef1fcac1e8890dba0b34b234ec2 (patch) | |
| tree | 8d3a29793d4d43bddba70abcaef071d40277daa0 /app | |
| parent | f96948724a0c157f98b952e6bd83da28d06ba1d9 (diff) | |
| download | inboxes-49e19f446e625ef1fcac1e8890dba0b34b234ec2.tar.bz2 | |
Speakers, new readme, dependencies and views generator
Diffstat (limited to 'app')
| -rw-r--r-- | app/controllers/discussions_controller.rb | 23 | ||||
| -rw-r--r-- | app/controllers/speakers_controller.rb | 26 | ||||
| -rw-r--r-- | app/models/discussion.rb | 2 | ||||
| -rw-r--r-- | app/models/speaker.rb | 5 | ||||
| -rw-r--r-- | app/views/discussions/_form.html.haml | 6 | ||||
| -rw-r--r-- | app/views/discussions/show.html.haml | 24 |
6 files changed, 62 insertions, 24 deletions
diff --git a/app/controllers/discussions_controller.rb b/app/controllers/discussions_controller.rb index eb91cfc..cf658b6 100644 --- a/app/controllers/discussions_controller.rb +++ b/app/controllers/discussions_controller.rb @@ -5,23 +5,16 @@ class DiscussionsController < ApplicationController before_filter :load_and_check_discussion_recipient, :only => [:create, :new] def index - # показываем дискуссии юзера, и те куда его присоеденили - # так как имеем массив дискуссий, его пагинация будет через хак @discussions = current_user.discussions - - respond_to do |format| - format.html # index.html.erb - format.json { render :json => @discussions } - end end # GET /discussions/1 # GET /discussions/1.json def show @discussion = Discussion.includes(:messages, :speakers).find(params[:id]) - redirect_to root_url, :notice => t("views.discussions.can_not_participate") unless @discussion.can_participate?(current_user) + redirect_to discussions_url, :notice => t("views.discussions.can_not_participate") unless @discussion.can_participate?(current_user) - @discussion.mark_as_read_for(current_user) # сделаем прочтенной для пользователя + @discussion.mark_as_read_for(current_user) end # GET /discussions/new @@ -42,7 +35,6 @@ class DiscussionsController < ApplicationController m.user = current_user end - if @discussion.save redirect_to @discussion, :notice => t("views.discussions.started") else @@ -58,17 +50,18 @@ class DiscussionsController < ApplicationController private def load_and_check_discussion_recipient + # initializing model fir new and create actions @discussion = Discussion.new((params[:discussion] ? params[:discussion] : {})) - @discussion.recipient_tokens = params[:recipients] if params[:recipients] + # @discussion.recipient_tokens = params[:recipients] if params[:recipients] # pre-population - # проверка, существует ли уже дискуссия с этим человеком + # checking if discussion with this user already exists if @discussion.recipient_ids && @discussion.recipient_ids.size == 1 user = User.find(@discussion.recipient_ids.first) discussion = Discussion.find_between_users(current_user, user) if discussion - # дискуссия уже существует, добавим в нее написанное сообщение - @discussion.messages.each do |m| - Message.create!(:discussion => discussion, :user => current_user, :body => m.body) if m.body + # it exists, let's add message and redirect current user + @discussion.messages.each do |message| + Message.create(:discussion => discussion, :user => current_user, :body => message.body) if message.body end # перекидываем на нее redirect_to discussion_url(discussion), :notice => t("views.discussions.exists", :user => user[Inboxes::config.user_name]) diff --git a/app/controllers/speakers_controller.rb b/app/controllers/speakers_controller.rb new file mode 100644 index 0000000..7fd5f1e --- /dev/null +++ b/app/controllers/speakers_controller.rb @@ -0,0 +1,26 @@ +class SpeakersController < ApplicationController + before_filter :init_and_check_permissions + + def create + # check permissions + raise ActiveRecord::RecordNotFound unless params[:speaker] && params[:speaker][:user_id] + @user = User.find(params[:speaker][:user_id]) + + flash[:notice] = t("views.speakers.added") if @discussion.add_speaker(@user) + redirect_to @discussion + end + + def destroy + @speaker = Speaker.find(params[:id]) + @speaker.destroy + flash[:notice] = t("views.speakers.removed") + redirect_to @discussion.speakers.any? ? @discussion : discussions_url + end + + private + + def init_and_check_permissions + @discussion = Discussion.find(params[:discussion_id]) + redirect_to discussions_url, :notice => t("views.discussions.can_not_participate") unless @discussion.can_participate?(current_user) + end +end diff --git a/app/models/discussion.rb b/app/models/discussion.rb index 7898140..cc1da7a 100644 --- a/app/models/discussion.rb +++ b/app/models/discussion.rb @@ -105,7 +105,7 @@ class Discussion < ActiveRecord::Base private def find_speaker_by_user user - Speaker.find_by_discussion_id_and_user_id!(self.id, user.id) + Speaker.find_by_discussion_id_and_user_id(self.id, user.id) end def check_that_has_at_least_two_users diff --git a/app/models/speaker.rb b/app/models/speaker.rb index 6105220..86be7dc 100644 --- a/app/models/speaker.rb +++ b/app/models/speaker.rb @@ -6,6 +6,7 @@ class Speaker < ActiveRecord::Base validates :user, :discussion, :presence => true after_destroy :destroy_discussion_view + after_destroy :destroy_discussion private @@ -14,4 +15,8 @@ class Speaker < ActiveRecord::Base @view.destroy if @view end + def destroy_discussion + self.discussion.destroy unless self.discussion.speakers.any? + end + end diff --git a/app/views/discussions/_form.html.haml b/app/views/discussions/_form.html.haml index eab0db6..7ceee2b 100644 --- a/app/views/discussions/_form.html.haml +++ b/app/views/discussions/_form.html.haml @@ -1,10 +1,12 @@ = form_for @discussion do |f| - .entry + %div = f.label :recipient_tokens + %br = select_tag "discussion[recipient_tokens]", options_from_collection_for_select(User.all, :id, Inboxes::config.user_name), :multiple => true, :size => "4" - .entry + %div = f.fields_for :messages do |j| = j.label :body + %br = j.text_area :body, :label => "Сообщение" = f.submit 'Отправить'
\ No newline at end of file diff --git a/app/views/discussions/show.html.haml b/app/views/discussions/show.html.haml index 903d8fd..ecad61f 100644 --- a/app/views/discussions/show.html.haml +++ b/app/views/discussions/show.html.haml @@ -1,13 +1,25 @@ -%h3 - Chat with - = @discussion.users.map { |u| u[Inboxes::config.user_name] }.join(", ") +%h2 + Members: +- @discussion.speakers.each do |speaker| + %div + = speaker.user[Inboxes::config.user_name] + = link_to '(remove)', discussion_speaker_path(@discussion, speaker), :method => :delete -%h3 Messages +- available_users = User.all.map { |u| u unless @discussion.users.include?(u) }.delete_if { |w| w.nil? } +- if available_users.any? + = form_for Speaker.new, :url => discussion_speakers_path(@discussion) do |f| + = f.label :user_id, "Add speaker" + = f.collection_select :user_id, available_users, :id, :name + = f.submit "Add" + +%h2 Messages - @discussion.messages.each do |message| - %p + %div = "[#{message.user[Inboxes::config.user_name]}]" = message.body = "@ #{l(message.created_at)}" - + +%h3 Add message = render "messages/form" + = link_to "All discussions", discussions_path
\ No newline at end of file |
