From 9d99d340cfd3c55f21d38c1ba9f3f4574e40c46c Mon Sep 17 00:00:00 2001 From: Azul Date: Sun, 9 Dec 2012 13:19:54 +0100 Subject: basic form added to user settings, simple model created --- users/app/controllers/email_alias_controller.rb | 36 --------------------- users/app/controllers/email_aliases_controller.rb | 39 +++++++++++++++++++++++ users/app/helpers/email_aliases_helper.rb | 11 +++++++ users/app/models/email_alias.rb | 6 ++++ users/app/models/user.rb | 6 ++-- users/app/views/users/_email_aliases.html.haml | 10 ++++++ users/app/views/users/edit.html.haml | 1 + users/config/routes.rb | 4 ++- 8 files changed, 74 insertions(+), 39 deletions(-) delete mode 100644 users/app/controllers/email_alias_controller.rb create mode 100644 users/app/controllers/email_aliases_controller.rb create mode 100644 users/app/helpers/email_aliases_helper.rb create mode 100644 users/app/models/email_alias.rb create mode 100644 users/app/views/users/_email_aliases.html.haml diff --git a/users/app/controllers/email_alias_controller.rb b/users/app/controllers/email_alias_controller.rb deleted file mode 100644 index 979c8ad..0000000 --- a/users/app/controllers/email_alias_controller.rb +++ /dev/null @@ -1,36 +0,0 @@ -class EmailAliasesController < ApplicationController - - before_filter :fetch_user - - # get a list of email aliases for the given user? - def index - @aliases = @user.email_aliases - respond_with @aliases - end - - def create - @alias = @user.add_email_alias(params[:email_alias]) - flash[:notice] = t(:email_alias_created_successfully) unless @alias.errors - respond_with @alias - end - - def update - @alias = @user.get_email_alias(params[:id]) - @alias.set_email(params[:email_alias]) - flash[:notice] = t(:email_alias_updated_successfully) unless @alias.errors - respond_with @alias - end - - def destroy - @alias = @user.get_email_alias(params[:id]) - flash[:notice] = t(:email_alias_destroyed_successfully) - @alias.destroy - end - - protected - - def fetch_user - @user = User.find_by_param(params[:user_id]) - access_denied unless admin? or (@user == current_user) - end -end diff --git a/users/app/controllers/email_aliases_controller.rb b/users/app/controllers/email_aliases_controller.rb new file mode 100644 index 0000000..751df85 --- /dev/null +++ b/users/app/controllers/email_aliases_controller.rb @@ -0,0 +1,39 @@ +class EmailAliasesController < ApplicationController + + before_filter :fetch_user + + respond_to :html + + # get a list of email aliases for the given user? + def index + @aliases = @user.email_aliases + respond_with @aliases + end + + def create + @alias = @user.add_email_alias(params[:email_alias]) + flash[:notice] = t(:email_alias_created_successfully) unless @alias.errors + respond_with @alias, :location => edit_user_path(@user, :anchor => :email) + end + + def update + @alias = @user.get_email_alias(params[:id]) + @alias.set_email(params[:email_alias]) + flash[:notice] = t(:email_alias_updated_successfully) unless @alias.errors + respond_with @alias, :location => edit_user_path(@user, :anchor => :email) + end + + def destroy + @alias = @user.get_email_alias(params[:id]) + flash[:notice] = t(:email_alias_destroyed_successfully) + @alias.destroy + redirect_to edit_user_path(@user, :anchor => :email) + end + + protected + + def fetch_user + @user = User.find_by_param(params[:user_id]) + access_denied unless admin? or (@user == current_user) + end +end diff --git a/users/app/helpers/email_aliases_helper.rb b/users/app/helpers/email_aliases_helper.rb new file mode 100644 index 0000000..b56b068 --- /dev/null +++ b/users/app/helpers/email_aliases_helper.rb @@ -0,0 +1,11 @@ +module EmailAliasesHelper + + def email_alias_form(options = {}) + simple_form_for [@user, EmailAlias.new()], + :html => {:class => "form-horizontal email-alias form"}, + :validate => true do |f| + yield f + end + end + +end diff --git a/users/app/models/email_alias.rb b/users/app/models/email_alias.rb new file mode 100644 index 0000000..25e4b27 --- /dev/null +++ b/users/app/models/email_alias.rb @@ -0,0 +1,6 @@ +class EmailAlias + include CouchRest::Model::Embeddable + + property :email, String + timestamps! +end diff --git a/users/app/models/user.rb b/users/app/models/user.rb index ae271ce..81d5262 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -1,11 +1,13 @@ class User < CouchRest::Model::Base property :login, String, :accessible => true - property :email, String, :accessible => true - property :email_forward, String, :accessible => true property :password_verifier, String, :accessible => true property :password_salt, String, :accessible => true + property :email, String, :accessible => true + property :email_forward, String, :accessible => true + property :email_aliases, [EmailAlias] + validates :login, :password_salt, :password_verifier, :presence => true diff --git a/users/app/views/users/_email_aliases.html.haml b/users/app/views/users/_email_aliases.html.haml new file mode 100644 index 0000000..54eac0f --- /dev/null +++ b/users/app/views/users/_email_aliases.html.haml @@ -0,0 +1,10 @@ +%legend= t(:email_aliases) +%ul + - @user.email_aliases.each do |email| + %li= email += email_alias_form do |f| + =f.input :email, :placeholder => "alias@#{request.domain}" + .pull-right + %button{:type => :submit, :class => 'btn'} + %i.icon-plus + Add Email Alias diff --git a/users/app/views/users/edit.html.haml b/users/app/views/users/edit.html.haml index b33c19b..eb1bca4 100644 --- a/users/app/views/users/edit.html.haml +++ b/users/app/views/users/edit.html.haml @@ -14,3 +14,4 @@ .tab-pane#email = user_form_with 'email_field', :legend => :set_email_address = user_form_with 'email_forward_field', :legend => :forward_email + = render 'email_aliases' diff --git a/users/config/routes.rb b/users/config/routes.rb index 6de216f..3c5fb73 100644 --- a/users/config/routes.rb +++ b/users/config/routes.rb @@ -10,6 +10,8 @@ Rails.application.routes.draw do resources :sessions, :only => [:new, :create, :update, :destroy] get "signup" => "users#new", :as => "signup" - resources :users + resources :users do + resources :email_aliases + end end -- cgit v1.2.3