summaryrefslogtreecommitdiff
path: root/app/controllers/phone_books_controller.rb
blob: 5f2d61f613b22a78f4b915c264b59994a9fadb1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
class PhoneBooksController < ApplicationController
  load_resource :user
  load_resource :user_group
  load_resource :tenant
  load_and_authorize_resource :phone_book, :through => [:user, :user_group, :tenant]

  before_filter :set_and_authorize_parent
  before_filter :spread_breadcrumbs
 
  def index
  end

  def show
    @by_name = params[:name]
    
    @pagination_page_number = params[:page].to_i
    @pagination_page_number = 1 if @pagination_page_number < 1
    
    if @by_name.blank?
      @phone_book_entries = @phone_book.
                            phone_book_entries.
                            order([ :last_name, :first_name ]).
                            paginate(
                              :page => @pagination_page_number,
                              :per_page => GsParameter.get('DEFAULT_PAGINATION_ENTRIES_PER_PAGE')
                            )
    else
      # search by name
      @by_name = @by_name.
        gsub( /[^A-Za-z0-9#]/, '' ).
        gsub('*','?').
        gsub('%','_').
        gsub(/^#/,'').
        upcase
      
      @phone_book_entries = @phone_book.
                            phone_book_entries.
                            where( '( ( last_name LIKE ? ) OR ( first_name LIKE ? ) )', "#{@by_name}%", "#{@by_name}%" ).
                            order([ :last_name, :first_name ]).
                            paginate(
                              :page => @pagination_page_number,
                              :per_page => GsParameter.get('DEFAULT_PAGINATION_ENTRIES_PER_PAGE')
                            )
    end
  end

  def new
    @phone_book = @parent.phone_books.build
    @phone_book.name = generate_a_new_name(@parent, @phone_book)
  end

  def create
    @phone_book = @parent.phone_books.build( params[:phone_book] )
    if @phone_book.save
      m = method( :"#{@parent.class.name.underscore}_phone_book_path" )
      redirect_to m.( @parent, @phone_book ), :notice => t('phone_books.controller.successfuly_created')
    else
      render :new
    end
  end

  def edit
  end

  def update
    if @phone_book.update_attributes(params[:phone_book])
      m = method( :"#{@parent.class.name.underscore}_phone_book_path" )
      redirect_to m.( @parent, @phone_book ), :notice  => t('phone_books.controller.successfuly_updated')
    else
      render :edit
    end
  end

  def destroy
    @phone_book.destroy
    m = method( :"#{@parent.class.name.underscore}_phone_books_url" )
    redirect_to m.( @parent ), :notice => t('phone_books.controller.successfuly_destroyed')
  end

  private
  def set_and_authorize_parent
    @parent = @user || @user_group || @tenant
    authorize! :read, @parent
  end

  def spread_breadcrumbs
    if @parent.class == Tenant
      add_breadcrumb t("phone_books.index.page_title"), tenant_phone_books_path(@tenant)
      if @phone_book && !@phone_book.new_record?
        add_breadcrumb @phone_book, tenant_phone_book_path(@tenant, @phone_book)
      end
    end

    if @parent.class == User
      add_breadcrumb t("users.index.page_title"), tenant_users_path(@user.current_tenant)
      add_breadcrumb @user, tenant_user_path(@user.current_tenant, @user)
      add_breadcrumb t("phone_books.index.page_title"), user_phone_books_path(@user)
      if @phone_book && !@phone_book.new_record?
        add_breadcrumb @phone_book, user_phone_book_path(@user, @phone_book)
      end
    end

  end

end