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
# 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]       unless person[:first_name].blank?
  @person.last_name        = person[:last_name]        unless person[:last_name].blank?
  @person.email            = person[:email]            unless person[:email].blank?
  @person.subscribed_lists = person[:subscribed_lists] unless person[:subscribed_lists].blank?
  @person.do_not_email     = person[:do_not_email]     unless person[:do_not_email].blank?
  @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_url(@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

#editObject



114
115
116
117
# File 'app/controllers/people_controller.rb', line 114

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

#indexObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/people_controller.rb', line 77

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

  if is_search(params)
    @people = Person.search_index(params[:search].dup, current_user.current_organization)
    respond_with do |format|
      format.csv  { render :csv => @people, :filename => "SearchResults-#{DateTime.now.strftime("%m-%d-%y")}.csv" }
      format.html { render :partial => 'list', :layout => false, :locals => { :people => @people } if request.xhr? }
      format.json { render :json => @people }
    end
  else
    @people = Person.recent(current_user.current_organization)
  end

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

#newObject



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

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

#showObject



95
96
97
98
99
# File 'app/controllers/people_controller.rb', line 95

def show
  @person = Person.find(params[:id])
  @orders = Order.where(:person_id => @person.id).includes(:person, :actions, :items).order(:created_at).paginate(:page => params[:page], :per_page => 25)
  authorize! :view, @person
end

#starObject



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

def star
  render :nothing => true
  type = params[:type]
  starable = Action.find(params[:action_id])

  if starable.starred?
    starable.starred = false
  else
    starable.starred = true
  end
  starable.save
end

#tagObject



119
120
121
122
123
124
125
# File 'app/controllers/people_controller.rb', line 119

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

#untagObject



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

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

#updateObject



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

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

  results = @person.update_attributes(params[:person])

  respond_to do |format|
    format.html do
      if results
        @person.create_subscribed_lists_notes!(current_user)
        flash[:notice] = "Your changes have been saved"
        @person = Person.find(params[:id])
        redirect_to person_url(@person)
      else
        flash[:alert] = "Sorry, we couldn't save your changes. Make sure you entered a first name, last name or email address."
        render :edit
      end
    end

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

end