Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User Subscriptions feature starting point #171

Merged
merged 10 commits into from
Dec 19, 2016
Prev Previous commit
Next Next commit
Integrate user Subscription in Consultations module
  • Loading branch information
danguita committed Dec 19, 2016
commit b6a3e52c94336137e5403e5efde9da1f5e31ad41
2 changes: 2 additions & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
@import "comp-graph-treemap";
@import "comp-graph-vis_lines_table";

@import "comp-user-subscriptions";

@import "module-budget_vis";
@import "module-participation";
@import "module-consultations";
Expand Down
10 changes: 10 additions & 0 deletions app/assets/stylesheets/comp-user-subscriptions.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.subscribable_box {
background: rgba($color_main, .1);
padding: 1.5rem;
margin-top: 4em;
color: $color_text;
h3 {
font-size: 1.3em;
margin: 0 0 1.25em 0;
}
}
58 changes: 58 additions & 0 deletions app/controllers/user/subscriptions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class User::SubscriptionsController < User::BaseController
before_action :authenticate_user!, only: [:index, :destroy]

def index
@user_subscriptions = find_user_subscriptions.sorted
end

def create
@user_subscription_form = User::SubscriptionForm.new(
user_subscription_params.merge(
user: current_user,
site: current_site,
creation_ip: remote_ip
)
)

if @user_subscription_form.save
flash[:notice] = t(".success")
else
flash[:alert] = t(
".error",
details: @user_subscription_form.errors.full_messages.to_sentence,
sign_in_path: new_user_sessions_path(domain: current_site.domain)
)
end

redirect_to request.referrer
end

def destroy
@user_subscription = find_user_subscription

@user_subscription.destroy

redirect_to user_subscriptions_path, notice: t(".success")
end

private

def find_user_subscription
find_user_subscriptions.find(params[:id])
end

def find_user_subscriptions
User::Subscription.where(
user: current_user,
site: current_site
)
end

def user_subscription_params
params.require(:user_subscription).permit(
:subscribable_type,
:subscribable_id,
:user_email
)
end
end
3 changes: 3 additions & 0 deletions app/forms/gobierto_admin/admin_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module GobiertoAdmin
class AdminForm
include ActiveModel::Model

EMAIL_ADDRESS_REGEXP = /\A(.+)@(.+\..+)\z/

attr_accessor(
:id,
:name,
Expand All @@ -20,6 +22,7 @@ class AdminForm
delegate :persisted?, to: :admin

validates :name, :email, presence: true
validates :email, format: { with: Admin::EMAIL_ADDRESS_REGEXP }
validates :password, presence: true, confirmation: true

def save
Expand Down
4 changes: 2 additions & 2 deletions app/forms/gobierto_admin/user_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class UserForm

delegate :persisted?, to: :user

validates :user, presence: true
validates :name, :email, presence: true
validates :user, :name, presence: true
validates :email, format: { with: User::EMAIL_ADDRESS_REGEXP }

def save
return false unless valid?
Expand Down
3 changes: 2 additions & 1 deletion app/forms/user/registration_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class User::RegistrationForm
:creation_ip
)

validates :email, :site, presence: true
validates :site, presence: true
validates :email, format: { with: User::EMAIL_ADDRESS_REGEXP }

def save
return false unless valid?
Expand Down
68 changes: 68 additions & 0 deletions app/forms/user/subscription_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
class User::SubscriptionForm
include ActiveModel::Model

attr_accessor(
:user,
:site,
:subscribable,
:subscribable_type,
:subscribable_id,
:user_email,
:creation_ip
)

validates :user, :site, :subscribable, presence: true
validates :user_email, format: { with: User::EMAIL_ADDRESS_REGEXP }, allow_nil: true

def save
create_user_subscription if valid?
end

def user
@user ||= begin
user_registration_form = User::RegistrationForm.new(
email: user_email,
site: site,
creation_ip: creation_ip
)

user_registration_form.save
promote_errors(user_registration_form.errors)
user_registration_form.user
end
end

def subscribable
@subscribable ||= begin
if @subscribable_id.present?
subscribable_class.find_by(id: @subscribable_id)
else
subscribable_class
end
end
end

def subscribable_type
@subscribable_type ||= @subscribable.try(:model_name)
end

def subscribable_id
@subscribable_id ||= @subscribable.try(:id)
end

private

def subscribable_class
subscribable_type.try(:constantize)
end

def create_user_subscription
user.subscribe_to!(subscribable, site)
end

def promote_errors(errors_hash)
errors_hash.each do |attribute, message|
errors.add(attribute, message)
end
end
end
4 changes: 1 addition & 3 deletions app/models/gobierto_admin/admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ class Admin < ApplicationRecord

before_create :set_god_flag

validates :name, presence: true
validates :email, presence: true, uniqueness: true,
format: { with: EMAIL_ADDRESS_REGEXP }
validates :email, uniqueness: true

scope :sorted, -> { order(created_at: :desc) }
scope :god, -> { where(god: true) }
Expand Down
2 changes: 2 additions & 0 deletions app/models/gobierto_budget_consultations/consultation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module GobiertoBudgetConsultations
class Consultation < ApplicationRecord
include User::Subscribable

belongs_to :admin, class_name: "GobiertoAdmin::Admin"
belongs_to :site

Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class User < ApplicationRecord
has_many :census_verifications, class_name: "User::Verification::CensusVerification"
has_many :subscriptions, dependent: :destroy

validates :email, presence: true, uniqueness: true, format: { with: EMAIL_ADDRESS_REGEXP }
validates :email, uniqueness: true

scope :sorted, -> { order(created_at: :desc) }
scope :by_source_site, ->(source_site) { where(source_site: source_site) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,8 @@

<% end %>

<%= render("user/subscriptions/subscribable_box",
subscribable: GobiertoBudgetConsultations::Consultation,
title: t(".subscribable_box.title")) %>

</div>
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,8 @@

<%= @consultation.description.html_safe %>

<%= render("user/subscriptions/subscribable_box",
subscribable: @consultation,
title: t(".subscribable_box.title")) %>

</div>
2 changes: 1 addition & 1 deletion app/views/user/shared/_flash_messages.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="flash-message <%= key %>">
<ul>
<li>
<%= value %>
<%= sanitize(value) %>
</li>
</ul>
</div>
Expand Down
33 changes: 33 additions & 0 deletions app/views/user/subscriptions/_subscribable_box.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<% user_subscription_form = User::SubscriptionForm.new(subscribable: subscribable) %>

<div class="subscribable_box">
<h3><%= title %></h3>

<% if user_signed_in? %>

<%= button_to(
t(".subscribe"),
user_subscriptions_path,
params: {
user_subscription: {
subscribable_type: user_subscription_form.subscribable_type,
subscribable_id: user_subscription_form.subscribable_id
}
},
class: "pure-menu-link")
%>

<% else %>

<%= form_for(user_subscription_form, as: :user_subscription, url: :user_subscriptions) do |f| %>
<%= f.hidden_field :subscribable_type %>
<%= f.hidden_field :subscribable_id %>
<%= f.email_field :user_email, placehlder: t(".placeholders.user_email") %>

<%= f.submit t(".submit") %>
<% end %>

<% end %>

<div class="disclaimer">No spam, date de baja cuando quieras</div>
</div>
30 changes: 30 additions & 0 deletions app/views/user/subscriptions/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<% title t(".title") %>

<% content_for :breadcrumb_current_item do %>
<strong>
<%= link_to t("user.layouts.menu_subsections.admin"), user_subscriptions_path %>
</strong>
<% end %>

<div class="column">
<table class="med_bg">
<tr>
<th><%= t(".headers.name") %></th>
<th></th>
</tr>
<tbody>
<% @user_subscriptions.each do |user_subscription| %>
<tr>
<td>
<% if user_subscription.specific? %>
<%= link_to user_subscription.subscribable_label, user_subscription.subscribable %>
<% elsif user_subscription.generic? %>
<%= user_subscription.subscribable_label %>
<% end %>
</td>
<td><%= link_to t(".cancel"), user_subscription, method: :delete, data: { confirm: "Are you sure?" } %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
5 changes: 5 additions & 0 deletions config/locales/gobierto_budget_consultations/models/ca.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
ca:
activerecord:
models:
gobierto_budget_consultations/consultation: Consulte
5 changes: 5 additions & 0 deletions config/locales/gobierto_budget_consultations/models/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
en:
activerecord:
models:
gobierto_budget_consultations/consultation: Consultation
5 changes: 5 additions & 0 deletions config/locales/gobierto_budget_consultations/models/es.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
es:
activerecord:
models:
gobierto_budget_consultations/consultation: Consulta
7 changes: 7 additions & 0 deletions config/locales/gobierto_budget_consultations/views/ca.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ ca:
layouts:
menu_subsections:
consultations: Consultes
consultations:
show:
subscribable_box:
title: Recibe actualizaciones en tu correo-e sobre cambios en esta consulta
index:
subscribable_box:
title: Recibe una alerta en tu correo-e sobre los procesos que se pongan en marcha
consultation_items:
options:
increase: Aumentar la despesa un 10%
Expand Down
7 changes: 7 additions & 0 deletions config/locales/gobierto_budget_consultations/views/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ en:
layouts:
menu_subsections:
consultations: Consultations
consultations:
show:
subscribable_box:
title: Receive updates in your inbox on this consultation
index:
subscribable_box:
title: Receive alerts in your inbox when there are any other processes underway
consultation_items:
options:
increase: Increase budget by 10%
Expand Down
7 changes: 7 additions & 0 deletions config/locales/gobierto_budget_consultations/views/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ es:
layouts:
menu_subsections:
consultations: Consultas
consultations:
show:
subscribable_box:
title: Recibe actualizaciones en tu correo-e sobre cambios en esta consulta
index:
subscribable_box:
title: Recibe una alerta en tu correo-e sobre los procesos que se pongan en marcha
consultation_items:
options:
increase: Aumentar el gasto un 10%
Expand Down
9 changes: 9 additions & 0 deletions config/locales/user/controllers/subscriptions/ca.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
ca:
user:
subscriptions:
create:
success: Se ha realizado la suscripción
error: "Ha ocurrido un problema al suscribirte: %{details}. Si ya estás registrado, identifícate <a href=\"%{sign_in_path}\">aquí</a>."
destroy:
success: Se ha cancelado esta suscripción
9 changes: 9 additions & 0 deletions config/locales/user/controllers/subscriptions/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
en:
user:
subscriptions:
create:
success: Subscription was successfully created
error: "There was a problem when subscribing to this resource: %{details}. If you are already registered, sign in <a href=\"%{sign_in_path}\">here</a>."
destroy:
success: Subscription was successfully canceled
9 changes: 9 additions & 0 deletions config/locales/user/controllers/subscriptions/es.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
es:
user:
subscriptions:
create:
success: Se ha realizado la suscripción
error: "Ha ocurrido un problema al suscribirte: %{details}. Si ya estás registrado, identifícate <a href=\"%{sign_in_path}\">aquí</a>."
destroy:
success: Se ha cancelado esta suscripción
Loading