Skip to content

Commit

Permalink
#667 add project deliveries
Browse files Browse the repository at this point in the history
  • Loading branch information
kalashnikovisme committed Jun 13, 2017
1 parent c5dcd6b commit 8c927f5
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ $ ->

$audience_type_input.change ->
value = $(this).val()
if value == 'event_registrations' || value == 'team'
if value == 'event_registrations' || value == 'team' || value == 'project'
$audience_select2.show()
$submit_button.prop 'disabled', true
url = Routes.api_admin_events_path() if value == 'event_registrations'
url = Routes.api_admin_teams_path() if value == 'team'
url = Routes.api_admin_projects_path() if value == 'project'
$audience_select2.select2
ajax:
url: url
Expand Down
10 changes: 10 additions & 0 deletions app/controllers/api/admin/projects_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Api::Admin::ProjectsController < Api::Admin::ApplicationController
def index
projects = if params[:q].present?
::Project.search_everywhere params[:q][:term]
else
::Project.all
end
render json: projects.to_json(only: [:id, :title])
end
end
2 changes: 1 addition & 1 deletion app/controllers/web/admin/delivery/campaigns_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def create
@campaign_form = ::Delivery::CampaignForm.new_with_model
@campaign_form.submit params[:delivery_campaign]
if @campaign_form.save
::Admin::FillReceiversJob.perform_later @campaign_form.model
::Admin::FillReceiversJob.perform_now @campaign_form.model
redirect_to admin_delivery_campaign_path(@campaign_form.model)
else
render action: :new
Expand Down
18 changes: 10 additions & 8 deletions app/helpers/web/admin/delivery/audiences_helper.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
module Web::Admin::Delivery::AudiencesHelper
def audience_path(audience)
case audience.audience_type
when 'users', 'contacts_emails'
''
when 'event_registrations'
# FIXME query from helper :(
link_to Event.find(audience.audience_id).title, admin_event_path(audience.audience_id)
when 'team'
link_to Team.find(audience.audience_id).title, admin_team_path(audience.audience_id)
end
when 'users', 'contacts_emails'
''
when 'event_registrations'
# FIXME query from helper :(
link_to Event.find(audience.audience_id).title, admin_event_path(audience.audience_id)
when 'team'
link_to Team.find(audience.audience_id).title, admin_team_path(audience.audience_id)
when 'project'
link_to Project.find(audience.audience_id).title, admin_project_path(audience.audience_id)
end
end

def audience_types_collection(campaign)
Expand Down
5 changes: 4 additions & 1 deletion app/models/delivery/audience.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
class Delivery::Audience < ActiveRecord::Base
extend Enumerize
belongs_to :campaign, class_name: 'Delivery::Campaign'
belongs_to :project

validates :audience_type, presence: true

enumerize :audience_type, in: [ :team, :users, :contact_emails, :event_registrations, :members ], default: :users
enumerize :audience_type, in: [ :team, :users, :contact_emails, :event_registrations, :members, :project ], default: :users

def contacts
@contacts ||= case audience_type
Expand All @@ -30,6 +31,8 @@ def contacts
when 'event_registrations'
event = Event.find audience_id
User.presented.subscribed_to_deliveries.with_email.where id: event.registrations.map(&:user_id)
when 'project'
User.with_email.presented.where id: Subscription.project(audience_id).map(&:user_id)
end
end
end
4 changes: 3 additions & 1 deletion app/models/subscription.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
class Subscription < ActiveRecord::Base
belongs_to :receiver, polymorphic: true
belongs_to :project

has_one :token, as: :record, dependent: :destroy

extend Enumerize
enumerize :subscription_type, in: [ :deliveries ]
enumerize :subscription_type, in: [ :deliveries, :project ]
enumerize :receiver_type, in: [ 'User', 'Delivery::ContactEmail' ]

include StateMachine::Scopes
Expand All @@ -17,6 +18,7 @@ class Subscription < ActiveRecord::Base
scope :news, -> { where record_type: 'News' }
scope :articles, -> { where record_type: 'Article' }
scope :empty, -> { active.where tag_type: :link, target_id: nil }
scope :project, -> (project_id) { where subscription_type: :project, project_id: project_id }

validates :subscription_type, uniqueness: { scope: [:receiver_id, :receiver_type] }
end
1 change: 1 addition & 0 deletions config/locales/ru/enumerize.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ ru:
team: На команду
event_registrations: Участников мероприятия
members: Члены организации
project: По проекту
logged_action:
action_type:
update: Обновил
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@
resources :teams, only: :index
resources :tags, only: [ :create, :destroy, :index ]
resources :documents, only: :create
resources :projects, only: :index
namespace :event do
resources :registrations, only: :create
end
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20170613070246_add_project_id_to_subscriptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddProjectIdToSubscriptions < ActiveRecord::Migration
def change
add_column :subscriptions, :project_id, :integer
end
end
13 changes: 8 additions & 5 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170611202422) do
ActiveRecord::Schema.define(version: 20170613070246) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -122,9 +122,9 @@
create_table "activity_lines_lider_ya_lider_participations", force: :cascade do |t|
t.integer "participant_id"
t.integer "stage_id"
t.text "state"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "state"
end

create_table "activity_lines_lider_ya_lider_stages", force: :cascade do |t|
Expand Down Expand Up @@ -283,9 +283,9 @@
create_table "delivery_receivers", force: :cascade do |t|
t.integer "user_id"
t.integer "campaign_id"
t.text "state"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "state", default: "ready"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "user_type"
end

Expand Down Expand Up @@ -609,6 +609,9 @@
t.integer "user_id"
end

add_index "teams_users", ["team_id"], name: "index_teams_users_on_team_id", using: :btree
add_index "teams_users", ["user_id"], name: "index_teams_users_on_user_id", using: :btree

create_table "test_paper_questions", force: :cascade do |t|
t.text "text"
t.integer "test_paper_id"
Expand Down
14 changes: 14 additions & 0 deletions test/controllers/api/admin/projects_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'test_helper'

class Api::Admin::ProjectsControllerTest < ActionController::TestCase
setup do
admin = create :admin
sign_in admin
create :place
end

test 'should get index' do
get :index
assert_response :success
end
end

0 comments on commit 8c927f5

Please sign in to comment.