diff options
| author | Marc Florisson | 2012-08-29 18:47:22 +0200 |
|---|---|---|
| committer | Marc Florisson | 2012-08-29 18:47:22 +0200 |
| commit | 61d0662dd923169c5c5aa1d4624f04d6e9dbe43d (patch) | |
| tree | 19e5943eb061cf1bb09709cb236fa10007064e32 | |
| parent | 6fd69ca92e3090a2a8b7b73e7511bc1070290e22 (diff) | |
| download | chouette-core-61d0662dd923169c5c5aa1d4624f04d6e9dbe43d.tar.bz2 | |
add subscription
| -rw-r--r-- | app/assets/images/icons/user.png | bin | 0 -> 2800 bytes | |||
| -rw-r--r-- | app/assets/stylesheets/organisations.css.scss | 4 | ||||
| -rw-r--r-- | app/assets/stylesheets/subscriptions.css.scss | 9 | ||||
| -rw-r--r-- | app/controllers/chouette_controller.rb | 2 | ||||
| -rw-r--r-- | app/controllers/referentials_controller.rb | 7 | ||||
| -rw-r--r-- | app/controllers/subscriptions_controller.rb | 20 | ||||
| -rw-r--r-- | app/helpers/users_helper.rb | 11 | ||||
| -rw-r--r-- | app/models/subscription.rb | 43 | ||||
| -rw-r--r-- | app/models/user.rb | 2 | ||||
| -rw-r--r-- | app/views/organisations/show.html.erb | 6 | ||||
| -rw-r--r-- | app/views/subscriptions/new.html.erb | 24 | ||||
| -rw-r--r-- | app/views/users/_form.html.erb | 9 | ||||
| -rw-r--r-- | app/views/users/show.html.erb | 4 | ||||
| -rw-r--r-- | config/locales/subscriptions.yml | 34 | ||||
| -rw-r--r-- | config/locales/users.yml | 43 | ||||
| -rw-r--r-- | config/routes.rb | 5 | ||||
| -rw-r--r-- | spec/factories.rb | 9 | ||||
| -rw-r--r-- | spec/requests/referentials_spec.rb | 10 | ||||
| -rw-r--r-- | spec/requests/stop_points_spec.rb | 1 | ||||
| -rw-r--r-- | spec/support/devise.rb | 6 | ||||
| -rw-r--r-- | spec/support/referential.rb | 6 |
21 files changed, 236 insertions, 19 deletions
diff --git a/app/assets/images/icons/user.png b/app/assets/images/icons/user.png Binary files differnew file mode 100644 index 000000000..06d0cd593 --- /dev/null +++ b/app/assets/images/icons/user.png diff --git a/app/assets/stylesheets/organisations.css.scss b/app/assets/stylesheets/organisations.css.scss index 13a078bd4..cb794940a 100644 --- a/app/assets/stylesheets/organisations.css.scss +++ b/app/assets/stylesheets/organisations.css.scss @@ -26,6 +26,10 @@ padding-right: 10px; position: relative; + img.preview { + width: 64px; + height: 64px; + } } } diff --git a/app/assets/stylesheets/subscriptions.css.scss b/app/assets/stylesheets/subscriptions.css.scss new file mode 100644 index 000000000..ca94fd80a --- /dev/null +++ b/app/assets/stylesheets/subscriptions.css.scss @@ -0,0 +1,9 @@ +// Place all the styles related to the lines controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ +@import "common"; + +#workspace.subscriptions.create, #workspace.subscriptions.new +{ +} + diff --git a/app/controllers/chouette_controller.rb b/app/controllers/chouette_controller.rb index 1b9f30a85..bc74f72fc 100644 --- a/app/controllers/chouette_controller.rb +++ b/app/controllers/chouette_controller.rb @@ -7,7 +7,7 @@ class ChouetteController < InheritedResources::Base end def referential - @referential ||= Referential.find params[:referential_id] + @referential ||= current_organisation.referentials.find params[:referential_id] end end diff --git a/app/controllers/referentials_controller.rb b/app/controllers/referentials_controller.rb index face0663f..6ee744413 100644 --- a/app/controllers/referentials_controller.rb +++ b/app/controllers/referentials_controller.rb @@ -6,5 +6,12 @@ class ReferentialsController < InheritedResources::Base show! end + protected + def resource + @referential ||= current_organisation.referentials.find_by_id(params[:id]) + end + def collection + @referentials ||= current_organisation.referentials + end end diff --git a/app/controllers/subscriptions_controller.rb b/app/controllers/subscriptions_controller.rb new file mode 100644 index 000000000..e860e94ec --- /dev/null +++ b/app/controllers/subscriptions_controller.rb @@ -0,0 +1,20 @@ +class SubscriptionsController < Devise::RegistrationsController + + def new + @subscription = Subscription.new + end + def create + @subscription = Subscription.new(params[:subscription]) + + if @subscription.save + sign_in @subscription.user + flash[:notice] = t('subscriptions.success') + redirect_to referentials_path + else + flash[:error] = t('subscriptions.failure') + render :action => "new" + end + end + +end + diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb new file mode 100644 index 000000000..ad45433c1 --- /dev/null +++ b/app/helpers/users_helper.rb @@ -0,0 +1,11 @@ +module UsersHelper + + def user_gravatar_image_tag(user) + gravatar_image_tag user.email, :alt => user.name, :class => "preview", :gravatar => { :default => user_default_avatar, :size => 64 } + end + + def user_default_avatar + "#{root_url}#{image_path('icons/user.png')}" + end + +end diff --git a/app/models/subscription.rb b/app/models/subscription.rb new file mode 100644 index 000000000..8b2b025de --- /dev/null +++ b/app/models/subscription.rb @@ -0,0 +1,43 @@ +class Subscription + include ActiveModel::Validations + include ActiveModel::Conversion + extend ActiveModel::Naming + + attr_accessor :organisation_name, :user_name, :email, :password, :password_confirmation + + def initialize(attributes = {}) + attributes.each do |name, value| + send("#{name}=", value) + end + end + + def persisted? + false + end + + def user + @user ||= organisation.users.build :email => email, :password => password, :password_confirmation => password_confirmation + end + + def organisation + @organisation ||= Organisation.new :name => organisation_name + end + + def valid? + unless organisation.valid? + self.errors.add( :organisation_name, organisation.errors[:name]) if organisation.errors[:name] + end + unless user.valid? + self.errors.add( :password, user.errors[:password]) if user.errors[:password] + self.errors.add( :email, user.errors[:email]) if user.errors[:email] + end + self.errors.empty? + end + + def save + if valid? + organisation.save and user.save + end + end + +end diff --git a/app/models/user.rb b/app/models/user.rb index 53853530f..4d6f72c0e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -5,7 +5,7 @@ class User < ActiveRecord::Base :recoverable, :rememberable, :trackable, :validatable # Setup accessible (or protected) attributes for your model - attr_accessible :email, :password, :password_confirmation, :remember_me + attr_accessible :email, :password, :password_confirmation, :remember_me, :name belongs_to :organisation diff --git a/app/views/organisations/show.html.erb b/app/views/organisations/show.html.erb index da895ea87..c407f56f9 100644 --- a/app/views/organisations/show.html.erb +++ b/app/views/organisations/show.html.erb @@ -2,8 +2,10 @@ <div class="users"> <% @organisation.users.each do |user| %> - <%= div_for(user) do %> - <li><%= link_to "#{user.email}", organisation_user_path(user) %></li> + <%= div_for user do %> + <%= user_gravatar_image_tag user %> + <%= link_to user.name, organisation_user_path(user) %> + <div class="info"><%= user.email %></div> <% end %> <% end %> </div> diff --git a/app/views/subscriptions/new.html.erb b/app/views/subscriptions/new.html.erb new file mode 100644 index 000000000..3d137a0d3 --- /dev/null +++ b/app/views/subscriptions/new.html.erb @@ -0,0 +1,24 @@ +<%= title_tag t('devise.registrations.new.title') %> + +<%= semantic_form_for @subscription, :url => subscription_path do |form| %> + <%= form.inputs do %> + <%= form.input :organisation_name %> + <%= form.input :email %> + <%= form.input :password, :as => :password %> + <%= form.input :password_confirmation, :as => :password %> + <% end %> + + <%= form.actions do %> + <%= form.action :submit, :as => :button, :label => t('devise.registrations.new.title') %> + <%= form.action :cancel, :as => :link %> + <% end %> +<% end %> + +<% content_for :sidebar do %> +<ul class="actions"> + <li> + <%= link_to t('devise.shared.sign_in'), new_user_session_path %> + </li> +</ul> +<% end %> + diff --git a/app/views/users/_form.html.erb b/app/views/users/_form.html.erb index 2894b5c90..3b19dddf2 100644 --- a/app/views/users/_form.html.erb +++ b/app/views/users/_form.html.erb @@ -1,10 +1,11 @@ <%= semantic_form_for [:organisation, @user] do |form| %> <%= form.inputs do %> + <%= form.input :name %> <%= form.input :email %> <% end %> - <%= form.actions do %> - <%= form.action :submit, :as => :button %> - <%= form.action :cancel, :as => :link %> - <% end %> + <%= form.actions do %> + <%= form.action :submit, :as => :button %> + <%= form.action :cancel, :as => :link %> + <% end %> <% end %> diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 30f5cc4ba..10c369bf4 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -2,6 +2,10 @@ <div class="user"> <p> +<label><%= User.human_attribute_name("name") %>: </label> +<%= @user.name %> +</p> +<p> <label><%= User.human_attribute_name("email") %>: </label> <%= @user.email %> </p> diff --git a/config/locales/subscriptions.yml b/config/locales/subscriptions.yml new file mode 100644 index 000000000..891a08d74 --- /dev/null +++ b/config/locales/subscriptions.yml @@ -0,0 +1,34 @@ +en: + subscriptions: + success: "Subscription saved" + failure: "Invalide subscription" + activemodel: + attributes: + subscription: + organisation_name: Organisation + user_name: User full name + subscriptions: + new: + title: Create your account + actions: + new: Create an account +fr: + subscriptions: + success: "Inscription enregistrée" + failure: "Inscription invalide" + activemodel: + models: + subscription: compte + attributes: + subscription: + organisation_name: "Organisation" + user_name: "Nom complet" + email: "Adresse mail" + password: "Mot de passe" + password_confirmation: "Confirmation du mot de passe" + current_password: "Mot de passe actuel" + subscriptions: + new: + title: Créer votre compte + actions: + new: Créer un compte diff --git a/config/locales/users.yml b/config/locales/users.yml new file mode 100644 index 000000000..075c23851 --- /dev/null +++ b/config/locales/users.yml @@ -0,0 +1,43 @@ +en: + users: + actions: + new: Add a new user + edit: Edit this user + destroy: Remove this user + destroy_confirm: Are you sure you want destroy this user? + new: + title: Add a new user + edit: + title: Update user %{user} + show: + title: User %{user} + index: + title: Users + activerecord: + models: + user: user + attributes: + user: + name: Full name +fr: + users: + actions: + new: Ajouter un utilisateur + edit: Modifier cet utilisateur + destroy: Supprimer cet utilisateur + destroy_confirm: Etes vous sûr de détruire cet utilisateur ? + new: + title: Ajouter un utilisateur + edit: + title: "Modifier l'utilisateur %{user}" + show: + title: Utilisateur %{user} + index: + title: Utilisateurs + activerecord: + models: + user: utilisateur + attributes: + user: + name: Nom complet + diff --git a/config/routes.rb b/config/routes.rb index 33f9c99f5..e9f574d5d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,8 @@ ChouetteIhm::Application.routes.draw do - devise_for :users + devise_for :users do + get "/users/sign_up" => "subscriptions#new" + post "/subscription" => "subscriptions#create" + end resource :organisation do resources :users diff --git a/spec/factories.rb b/spec/factories.rb index c530ea650..87387f3f5 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -1,17 +1,18 @@ FactoryGirl.define do + factory :organisation do |f| + f.sequence(:name) { |n| "Organisation #{n}" } + end + factory :referential do |f| f.sequence(:name) { |n| "Test #{n}" } f.sequence(:slug) { |n| "test_#{n}" } f.sequence(:prefix) { |n| "test_#{n}" } + f.association :organisation f.time_zone "Europe/Paris" end - factory :organisation do |f| - f.sequence(:name) { |n| "Organisation #{n}" } - end - factory :user do |f| f.association :organisation f.sequence(:email) { |n| "chouette#{n}@dryade.priv" } diff --git a/spec/requests/referentials_spec.rb b/spec/requests/referentials_spec.rb index 094add627..9203de16d 100644 --- a/spec/requests/referentials_spec.rb +++ b/spec/requests/referentials_spec.rb @@ -12,8 +12,14 @@ describe "Referentials" do end context "when several referentials exist" do + + def retrieve_referential_by_slug( slug) + @user.organisation.referentials.find_by_slug(slug) || + create(:referential, :slug => slug, :name => slug, :organisation => @user.organisation) + end - let!(:referentials) { Array.new(2) { create(:referential) } } + let!(:referentials) { [ retrieve_referential_by_slug("aa"), + retrieve_referential_by_slug("bb")] } it "should show n referentials" do pending @@ -44,7 +50,7 @@ describe "Referentials" do end describe "destroy" do - let(:referential) { create(:referential) } + let(:referential) { create(:referential, :organisation => @user.organisation) } it "should remove referential" do pending "Unauthorized DELETE (ticket #14)" diff --git a/spec/requests/stop_points_spec.rb b/spec/requests/stop_points_spec.rb index 87b34413c..f2805263d 100644 --- a/spec/requests/stop_points_spec.rb +++ b/spec/requests/stop_points_spec.rb @@ -20,6 +20,7 @@ describe "StopPoints" do end describe "from route's page, go to new stop point page" do it "display route's stop points" do + visit referential_line_route_path(referential,line,route) click_link "Gérer les arrêts de la séquence" click_link "Ajouter un arrêt à la séquence" diff --git a/spec/support/devise.rb b/spec/support/devise.rb index 444eb0fee..4e0e3a4d2 100644 --- a/spec/support/devise.rb +++ b/spec/support/devise.rb @@ -2,7 +2,8 @@ module DeviseRequestHelper include Warden::Test::Helpers def login_user - @user ||= create :user + organisation = Organisation.find_by_name("first") || create(:organisation, :name => "first") + @user ||= create(:user, :organisation => organisation) login_as @user, :scope => :user # post_via_redirect user_session_path, 'user[email]' => @user.email, 'user[password]' => @user.password end @@ -32,7 +33,8 @@ module DeviseControllerHelper def login_user before(:each) do @request.env["devise.mapping"] = Devise.mappings[:user] - sign_in create(:user) + organisation = Organisation.find_by_name("first") || create(:organisation, :name => "first") + sign_in create(:user, :organisation => organisation) end end end diff --git a/spec/support/referential.rb b/spec/support/referential.rb index 0d3a7b89a..892903228 100644 --- a/spec/support/referential.rb +++ b/spec/support/referential.rb @@ -1,7 +1,7 @@ module ReferentialHelper def first_referential - Referential.find_by_slug("first") + Organisation.find_by_name("first").referentials.find_by_slug("first") end def self.included(base) @@ -27,7 +27,9 @@ RSpec.configure do |config| config.include ReferentialHelper config.before(:suite) do - Referential.find_or_create_by_slug FactoryGirl.attributes_for(:referential, :slug => "first") + organisation = Organisation.find_or_create_by_name :name => "first" + organisation.referentials.find_by_slug("first" ) || + FactoryGirl(:referential, :slug => "first", :organisation => organisation) # FIXME in Rails 3.2 : # Referential.where(:slug => 'first').first_or_create(FactoryGirl.attributes_for(:referential)) end |
