diff options
| author | Marc Florisson | 2012-08-24 11:46:09 +0200 | 
|---|---|---|
| committer | Marc Florisson | 2012-08-24 11:46:09 +0200 | 
| commit | 6fd69ca92e3090a2a8b7b73e7511bc1070290e22 (patch) | |
| tree | 8f25b31f44fe9b239b25d232f8aee8844c7d0688 | |
| parent | 1fd4e99d38e25ba4e70324a2f06aa532ee0e7862 (diff) | |
| download | chouette-core-6fd69ca92e3090a2a8b7b73e7511bc1070290e22.tar.bz2 | |
create organisation
| -rw-r--r-- | app/assets/stylesheets/organisations.css.scss | 31 | ||||
| -rw-r--r-- | app/assets/stylesheets/users.css.scss | 8 | ||||
| -rw-r--r-- | app/controllers/application_controller.rb | 11 | ||||
| -rw-r--r-- | app/controllers/organisations_controller.rb | 10 | ||||
| -rw-r--r-- | app/controllers/users_controller.rb | 35 | ||||
| -rw-r--r-- | app/mailers/user_mailer.rb | 11 | ||||
| -rw-r--r-- | app/models/organisation.rb | 5 | ||||
| -rw-r--r-- | app/models/referential.rb | 2 | ||||
| -rw-r--r-- | app/models/user.rb | 6 | ||||
| -rw-r--r-- | app/views/layouts/application.html.erb | 1 | ||||
| -rw-r--r-- | app/views/organisations/show.html.erb | 16 | ||||
| -rw-r--r-- | app/views/users/_form.html.erb | 10 | ||||
| -rw-r--r-- | app/views/users/edit.html.erb | 3 | ||||
| -rw-r--r-- | app/views/users/new.html.erb | 3 | ||||
| -rw-r--r-- | app/views/users/show.html.erb | 17 | ||||
| -rw-r--r-- | config/environments/development.rb | 5 | ||||
| -rw-r--r-- | config/initializers/apartment.rb | 2 | ||||
| -rw-r--r-- | config/routes.rb | 4 | ||||
| -rw-r--r-- | spec/factories.rb | 5 | ||||
| -rw-r--r-- | spec/mailers/user_mailer_spec.rb | 21 | ||||
| -rw-r--r-- | spec/models/user_spec.rb | 18 | 
21 files changed, 222 insertions, 2 deletions
| diff --git a/app/assets/stylesheets/organisations.css.scss b/app/assets/stylesheets/organisations.css.scss new file mode 100644 index 000000000..13a078bd4 --- /dev/null +++ b/app/assets/stylesheets/organisations.css.scss @@ -0,0 +1,31 @@ +// 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.organisations.show +{ +    .user:after {  +        @include after_div_for_object;   +    } + +    .users { +        margin-top: 20px; +    } + +    .users:after { +        @include content_to_clear; +    } +     +    .user { +        @include div_for_object; + +        /* to create multi-column index */ +        width: 350px; +        float: left; +        padding-right: 10px; +        position: relative; + +    } +} + diff --git a/app/assets/stylesheets/users.css.scss b/app/assets/stylesheets/users.css.scss new file mode 100644 index 000000000..e0fef9e5c --- /dev/null +++ b/app/assets/stylesheets/users.css.scss @@ -0,0 +1,8 @@ +// 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.users.edit, #workspace.lines.new +{ +} diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ae953a25a..8cde89db0 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,4 +1,15 @@  class ApplicationController < ActionController::Base    protect_from_forgery    before_filter :authenticate_user! + +  protected + +  def current_organisation +    current_user.organisation if current_user +  end +  helper_method :current_organisation + +  def begin_of_association_chain +    current_organisation +  end  end diff --git a/app/controllers/organisations_controller.rb b/app/controllers/organisations_controller.rb new file mode 100644 index 000000000..daeb41837 --- /dev/null +++ b/app/controllers/organisations_controller.rb @@ -0,0 +1,10 @@ +class OrganisationsController < InheritedResources::Base +  respond_to :html + +  private + +  def resource +    @organisation = current_organisation +  end +end + diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 000000000..ee9cbcda1 --- /dev/null +++ b/app/controllers/users_controller.rb @@ -0,0 +1,35 @@ +class UsersController < InheritedResources::Base + +  def create +        Rails.logger.info( "call user_controller.create") +        Rails.logger.info( "resource=#{build_resource.inspect}") +        Rails.logger.info( "resourc.valid?e=#{build_resource.valid?}") +        Rails.logger.info( "resourc.errors=#{build_resource.errors.inspect}") +    create! do |success, failure| +      success.html {  +        Rails.logger.info( "success user_controller") +        mail = UserMailer.welcome(@user) +        mail.deliver +        redirect_to organisation_user_path(@user) } +    end +  end + +  def update +    update! do |success, failure| +      success.html { redirect_to organisation_user_path(@user) } +    end +  end + +  def destroy +    destroy! do |success, failure| +      success.html { redirect_to organisation_path } +    end +  end + +  protected +   +  def begin_of_association_chain +    current_organisation +  end + +end diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb new file mode 100644 index 000000000..4c831dae4 --- /dev/null +++ b/app/mailers/user_mailer.rb @@ -0,0 +1,11 @@ +class UserMailer < ActionMailer::Base +  default :from => 'sim@dryade.net' +   +  def welcome(user) +    @user = user +    mail(:subject => "Welcome to #{user.organisation.name}", +         :to => user.email) +  end + +end + diff --git a/app/models/organisation.rb b/app/models/organisation.rb new file mode 100644 index 000000000..28d592fc9 --- /dev/null +++ b/app/models/organisation.rb @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +class Organisation < ActiveRecord::Base +  has_many :users, :dependent => :destroy +  has_many :referentials, :dependent => :destroy +end diff --git a/app/models/referential.rb b/app/models/referential.rb index 953038168..b87ba81e8 100644 --- a/app/models/referential.rb +++ b/app/models/referential.rb @@ -20,6 +20,8 @@ class Referential < ActiveRecord::Base    has_many :imports, :dependent => :destroy    has_many :exports, :dependent => :destroy +   +  belongs_to :organisation    def slug_excluded_values      if ! slug.nil? diff --git a/app/models/user.rb b/app/models/user.rb index b2f7c8ec9..53853530f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -6,4 +6,10 @@ class User < ActiveRecord::Base    # Setup accessible (or protected) attributes for your model    attr_accessible :email, :password, :password_confirmation, :remember_me + +  belongs_to :organisation + +  before_validation(:on => :create) do +    self.password ||= Devise.friendly_token.first(6) +  end  end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 69d5be2a5..417f677c3 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -37,6 +37,7 @@            <% elsif ! selected_referential? %>              <li><%= link_to Referential.model_name.human(:count=>2), referentials_path, :class => ("current" if current_page?(referentials_path) || current_page?(root_url)) %></li>              <li><%= tab_link_to FileValidation.model_name.human(:count=>2), file_validations_path %></li> +            <li class="admin"><%= tab_link_to Organisation.model_name.human, organisation_path %></li>            <% else %>              <li><%= link_to t("layouts.tabs.dashboard"), referential_path(@referential), :class => ("current" if current_page?(referential_path(@referential))) %></li>              <li><%= tab_link_to Chouette::Network, referential_networks_path(@referential) %></li> diff --git a/app/views/organisations/show.html.erb b/app/views/organisations/show.html.erb new file mode 100644 index 000000000..da895ea87 --- /dev/null +++ b/app/views/organisations/show.html.erb @@ -0,0 +1,16 @@ +<%= title_tag @organisation.name %> + +<div class="users"> +  <% @organisation.users.each do |user| %> +    <%= div_for(user) do %> +    <li><%= link_to "#{user.email}", organisation_user_path(user) %></li> +    <% end %> +  <% end %> +</div> + +<% content_for :sidebar do %> +<ul class="actions"> +  <%= link_to t('users.actions.new'), new_organisation_user_path, :class => "add" %> +</ul> +<% end %> + diff --git a/app/views/users/_form.html.erb b/app/views/users/_form.html.erb new file mode 100644 index 000000000..2894b5c90 --- /dev/null +++ b/app/views/users/_form.html.erb @@ -0,0 +1,10 @@ +<%= semantic_form_for [:organisation, @user] do |form| %> +  <%= form.inputs do %>  +    <%= form.input :email %>  +  <% end %>   + +  <%= form.actions do %> +    <%= form.action :submit, :as => :button %> +    <%= form.action :cancel, :as => :link %> +  <% end %> +<% end %> diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb new file mode 100644 index 000000000..89d01b648 --- /dev/null +++ b/app/views/users/edit.html.erb @@ -0,0 +1,3 @@ +<%= title_tag t('.title', :user => @user.email) %> + +<%= render "form" %> diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb new file mode 100644 index 000000000..98986b4d6 --- /dev/null +++ b/app/views/users/new.html.erb @@ -0,0 +1,3 @@ +<%= title_tag t('.title') %> + +<%= render "form" %> diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb new file mode 100644 index 000000000..30f5cc4ba --- /dev/null +++ b/app/views/users/show.html.erb @@ -0,0 +1,17 @@ +<%= title_tag @user.email %> + +<div class="user"> +<p> +<label><%= User.human_attribute_name("email") %>: </label> +<%= @user.email %> +</p> +</div> + +<% content_for(:sidebar) do %> +  <ul class="actions"> +    <li><%= link_to t('users.actions.edit'), edit_organisation_user_path( @user), :class => "edit" %></li> +    <% unless current_user.id==@user.id %> +    <li><%= link_to t('users.actions.destroy'), organisation_user_path(@user),:method => :delete, :confirm => t('users.actions.destroy_confirm'), :class => "remove" %></li> +    <% end %> +  </ul> +<% end %> diff --git a/config/environments/development.rb b/config/environments/development.rb index 06b340db2..a870ebd7d 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -34,6 +34,11 @@ ChouetteIhm::Application.configure do    config.action_mailer.default_url_options = { :host => 'localhost:3000' } +  ActionMailer::Base.smtp_settings = { +    :address        => "mail.dryade.priv", +    :domain         => "dryade.priv" +  } +    config.to_prepare do      chouette_command_script = "tmp/chouette-command/chouette"      if File.exists? chouette_command_script diff --git a/config/initializers/apartment.rb b/config/initializers/apartment.rb index 7d1e93622..bff88d5fc 100644 --- a/config/initializers/apartment.rb +++ b/config/initializers/apartment.rb @@ -1,6 +1,6 @@  Apartment.configure do |config|    # set your options (described below) here -  config.excluded_models = ["Referential", "User", "Import", "ImportLogMessage", "Export", "ExportLogMessage", "Delayed::Backend::ActiveRecord::Job"]        # these models will not be multi-tenanted, but remain in the global (public) namespace +  config.excluded_models = ["Referential", "Organisation", "User", "Import", "ImportLogMessage", "Export", "ExportLogMessage", "Delayed::Backend::ActiveRecord::Job"]        # these models will not be multi-tenanted, but remain in the global (public) namespace    # Dynamically get database names to migrate    config.database_names = lambda{ Referential.pluck(:slug) } diff --git a/config/routes.rb b/config/routes.rb index 2d2b7ba81..33f9c99f5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,10 @@  ChouetteIhm::Application.routes.draw do    devise_for :users +  resource :organisation do +    resources :users +  end +    resources :file_validations    resources :referentials do diff --git a/spec/factories.rb b/spec/factories.rb index 8e62b3b64..c530ea650 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -8,7 +8,12 @@ FactoryGirl.define do      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" }      f.password "secret"      f.password_confirmation "secret" diff --git a/spec/mailers/user_mailer_spec.rb b/spec/mailers/user_mailer_spec.rb new file mode 100644 index 000000000..2471de988 --- /dev/null +++ b/spec/mailers/user_mailer_spec.rb @@ -0,0 +1,21 @@ +require "spec_helper" + +describe UserMailer do + +  describe "welcome" do +    let(:user) {Factory(:user)} + +    it "should verify if email send" do  +      email = UserMailer.welcome(user).deliver +      ActionMailer::Base.deliveries.empty?.should be_false +    end +     +    it "should verify the content of sending email" do +      email = UserMailer.welcome(user).deliver +      email.to.should == [user.email] +      email.subject.should == "Welcome to #{user.organisation.name}" +    end + +  end + +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 44032b484..dbf95a120 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1,5 +1,21 @@  require 'spec_helper'  describe User do -  pending "add some examples to (or delete) #{__FILE__}" +  #it { should validate_uniqueness_of :email } +  #it { should validate_presence_of :name } + +  describe "#destroy" do +    let!(:organisation){Factory(:organisation)} +    let!(:user){Factory(:user, :organisation => organisation)} +    context "user's organisation contains many user" do +      let!(:other_user){Factory(:user, :organisation => organisation)} +      it "should destoy also user's organisation" do +        user.destroy +        Organisation.where(:name => organisation.name).exists?.should be_true +        read_organisation = Organisation.where(:name => organisation.name).first +        read_organisation.users.count.should == 1 +        read_organisation.users.first.should == other_user +      end +    end +  end  end | 
