diff options
| -rw-r--r-- | README.md | 10 | ||||
| -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 | ||||
| -rw-r--r-- | config/locales/en.yml | 7 | ||||
| -rw-r--r-- | config/locales/ru.yml | 17 | ||||
| -rw-r--r-- | inboxes.gemspec | 2 | ||||
| -rw-r--r-- | lib/generators/inboxes/views_generator.rb | 13 | ||||
| -rw-r--r-- | lib/inboxes/active_record_extension.rb | 2 | 
12 files changed, 99 insertions, 38 deletions
| @@ -24,13 +24,19 @@ Remember that unfortunately, Inboxes reserve 3 model names: Discussion, Discussi  4. Now Inboxes is ready to use. Open `http://yoursite.dev/discussions` to show discussions list. You can start new one.  Default Inboxes views are ugly, so you can copy it to your app and make anything with them: `rails generate inboxes:views` +If you have problems with installation if gem, you can check [demo app code](https://github.com/kirs/inboxes-app) -###Todo +## I18n + +By default, this gem provides localized phrases for Russian and English languages. You can easily override any of them. [Here is](https://github.com/kirs/inboxes/blob/master/config/locales/en.yml) list of all I18n phrases. + +##Todo  - Add rspec tests  - Move gem resources to namespace +- Describe integration with Faye -###Authors: +##Authors  - [Kir Shatrov](https://github.com/kirs/) (Evrone Company) 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 diff --git a/config/locales/en.yml b/config/locales/en.yml index fa7803a..34906af 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1,5 +1,12 @@  en: +  activerecord: +    attributes: +      discussion: +        recipient_tokens: "Recipients"    views: +    speakers: +      added: "Speaker was added to discussion" +      removed: "Speaker was removed from discussion"      discussions:        started: "Discussion started"        leaved: "You leaved discussion" diff --git a/config/locales/ru.yml b/config/locales/ru.yml index 06d3534..cc4844e 100644 --- a/config/locales/ru.yml +++ b/config/locales/ru.yml @@ -1,7 +1,16 @@  ru: +  activerecord: +    attributes: +      discussion: +        recipient_tokens: "Получатели" +      message: +        body: "Сообщение"    views:      discussions: -      started: "Чат начат" -      leaved: "Вы покинули дискуссию" -      exists: "Дискуссия между вами и %{user} уже существует" -      can_not_participate: "Вы не состоите в этой дискуссии"
\ No newline at end of file +      started: "Чат начат." +      leaved: "Вы покинули дискуссию." +      exists: "Дискуссия между вами и %{user} уже существует." +      can_not_participate: "Вы не состоите в этой дискуссии." +    speakers: +      added: "Участник дискуссии добавлен." +      removed: "Участник дискуссии удален."
\ No newline at end of file diff --git a/inboxes.gemspec b/inboxes.gemspec index 441f0a5..ff3c296 100644 --- a/inboxes.gemspec +++ b/inboxes.gemspec @@ -20,5 +20,5 @@ Gem::Specification.new do |s|    # specify any dependencies here; for example:    s.add_development_dependency "ruby-debug" -  # s.add_runtime_dependency "rest-client" +  s.add_runtime_dependency "haml-rails"  end diff --git a/lib/generators/inboxes/views_generator.rb b/lib/generators/inboxes/views_generator.rb index 07200ae..67ce55e 100644 --- a/lib/generators/inboxes/views_generator.rb +++ b/lib/generators/inboxes/views_generator.rb @@ -7,11 +7,14 @@ module Inboxes        class_option :template_engine, :type => :string, :aliases => '-e', :desc => 'Template engine for the views. Available options are "erb" and "haml".'        def copy_or_fetch -        # filename_pattern = File.join self.class.source_root, "discussions", "*.html.#{template_engine}" -        # puts Dir.glob(filename_pattern).map {|f| File.basename f}.inspect -        # Dir.glob(filename_pattern).map {|f| File.basename f}.each do |f| -        #   # copy_file f, "app/views/#{f}" -        # end +        # templates =  +        # [ +        #   "discussions/" +        # ] +        filename_pattern = File.join self.class.source_root, "*" #/*.html.#{template_engine}" +        Dir.glob(filename_pattern).map {|f| File.basename f}.each do |f| +          directory f.to_s, "app/views/#{f}" +        end        end        private diff --git a/lib/inboxes/active_record_extension.rb b/lib/inboxes/active_record_extension.rb index 1cb94f2..52b6810 100644 --- a/lib/inboxes/active_record_extension.rb +++ b/lib/inboxes/active_record_extension.rb @@ -6,8 +6,6 @@ module Inboxes        has_many :speakers, :dependent => :destroy        has_many :discussions, :through => :speakers -       -      # scope "#{prefix}_#{name}", lambda {|value| where("#{field}" => value) }      end    end  end
\ No newline at end of file | 
