Class: PeopleController

Inherits:
ArtfullyOseController show all
Defined in:
app/controllers/people_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



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
# File 'app/controllers/people_controller.rb', line 10

def create
  authorize! :create, Person
  @person = Person.new
  person = params[:person]

  @person.first_name       = person[:first_name]        if person[:first_name].present?
  @person.last_name        = person[:last_name]         if person[:last_name].present?
  @person.email            = person[:email]             if person[:email].present?
  @person.company_name     = person[:company_name]      if person[:company_name].present?
  @person.subscribed_lists = person[:subscribed_lists]  if person[:subscribed_lists].present?
  @person.do_not_email     = person[:do_not_email]      if person[:do_not_email].present?
  @person.do_not_call      = person[:do_not_call]       if person[:do_not_call].present?
  @person.type             = person[:type] || "Individual"
  @person.subtype          = person[:subtype] || "Individual"
  @person.organization_id  = current_user.current_organization.id

  if @person.valid? && @person.save!
    @person.create_subscribed_lists_notes!(current_user)

    respond_to do |format|
      format.html do
        redirect_to_person(@person)
      end

      format.json do
        render :json => @person.as_json
      end
    end
  else
    respond_to do |format|
      format.html do
        render :new
      end

      format.json do
        render :json => @person.as_json.merge(:errors => @person.errors.full_messages), :status => 400
      end
    end
  end
end

#destroyObject



132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/controllers/people_controller.rb', line 132

def destroy
  @person = Person.find(params[:id])
  authorize! :destroy, @person

  if @person.destroy
    flash[:notice] = "The person has been deleted."
    redirect_to people_path
  else
    flash[:alert] = "The person could not be deleted."
    redirect_to_person(@person)
  end
end

#editObject



127
128
129
130
# File 'app/controllers/people_controller.rb', line 127

def edit
  @person = Person.find(params[:id])
  authorize! :edit, @person
end

#indexObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/controllers/people_controller.rb', line 81

def index
  authorize! :manage, Person
  @people = []
  @show_advanced_search_message = false

  if is_search(params)
    @people = Person.search_index(params[:search].dup, current_user.current_organization)
    @show_advanced_search_message = @people.length > 20
  else
    @people = Person.recent(current_user.current_organization)
  end

  @people = @people.paginate(:page => params[:page], :per_page => 20)

  respond_with do |format|
    format.html { render :index }
    format.json { render :json => @people } #inline people search depends on json response
  end
end

#newObject



5
6
7
8
# File 'app/controllers/people_controller.rb', line 5

def new
  authorize! :create, Person
  @person = Person.new
end

#reset_passwordObject

Weird to put here, but otherwise it’ll collide with Devise’s MembersController



164
165
166
167
168
169
170
171
172
173
174
# File 'app/controllers/people_controller.rb', line 164

def reset_password
  @person = Person.find(params[:id])
  authorize! :view, @person
  @member = @person.member

  return if @member.nil?

  @member.delay.send_reset_password_instructions
  flash[:notice] = "We'll get that password reset email out to #{@member.email} right away!"
  redirect_to person_memberships_path(@member.person)
end

#showObject



101
102
103
104
105
106
107
108
# File 'app/controllers/people_controller.rb', line 101

def show
  @person = Person.find(params[:id])
  @notes = @person.notes.order('starred desc').order('updated_at desc')
  @actions = @person.actions.includes(:subject).order('starred desc').order('occurred_at desc').page(params[:page]).per_page(20)
  @person.build_address unless @person.address
  @new_action = Action.for_organization(current_user.current_organization)
  authorize! :view, @person
end

#starObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/controllers/people_controller.rb', line 110

def star
  render :nothing => true
  @person = Person.find(params_person_id)
  authorize! :edit, @person

  type = params[:type]
  starable = type.classify.constantize.find(params[:action_id])

  if type == 'relationship'
    starable.starred ? starable.unstar! : starable.star!
  else
    starable.starred = ! starable.starred?
    starable.save
  end

end

#tagObject



145
146
147
148
149
150
151
# File 'app/controllers/people_controller.rb', line 145

def tag
  @person = Person.find(params_person_id)
  authorize! :edit, @person
  @person.tag_list << params[:tag]
  @person.save
  render :nothing => true
end

#untagObject



153
154
155
156
157
158
159
# File 'app/controllers/people_controller.rb', line 153

def untag
  @person = Person.find(params_person_id)
  authorize! :edit, @person
  @person.tag_list.remove(params[:tag])
  @person.save
  render :nothing => true
end

#updateObject



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
# File 'app/controllers/people_controller.rb', line 51

def update
  @person = Person.find(params[:id])
  authorize! :edit, @person
  flash[:notice] = []
  results = @person.update_attributes(person_update_params)
  @person.relationships.where(:inverse_id => nil).map(&:ensure_inverse)

  respond_to do |format|
    format.html do
      if results
        @person.create_subscribed_lists_notes!(current_user)
        flash[:notice] << "Your changes have been saved"
      else
        errs = [@person.errors.delete(:"relationships.base")].flatten + [@person.errors.full_messages.to_sentence].flatten
        flash[:alert] = errs.blank? ? "Sorry, we couldn't save your changes. Make sure you entered a first name, last name or email address." : errs.compact
      end
      redirect_to_person(@person, params)
    end

    format.json do
      if results
        render :json => @person
      else
        render :nothing => true
      end
    end
  end

end