/
contacts_controller.rb
151 lines (135 loc) · 4.23 KB
/
contacts_controller.rb
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
class ContactsController < ApplicationController
load_and_authorize_resource
before_action :set_contact, only: [:show, :edit, :update, :destroy]
after_action :set_birth_month, only: [:create, :update]
def index
@q = Contact.search(params[:q])
@contacts = @q.result.order('authenticated ASC, id DESC').includes(:participated_groups).paginate(:page => params[:page], :per_page => 50)
respond_to do |format|
format.json
format.html # /app/views/contacts/index.html.erb
format.html.phone # /app/views/contacts/index.html+phone.erb
format.html.pad # /app/views/contacts/index.html+pad.erb
end
end
def show
end
def new
@contact = Contact.new
@my_ip = my_ip
end
def create
if 1 == params[:contact][:unknown_year]
params[:contact]["birthday(1i)"]=1900.to_s
end
if 1 == params[:contact][:unknown_birthday]
params[:contact]["birthday(1i)"]=1900.to_s
params[:contact]["birthday(2i)"]=1.to_s
params[:contact]["birthday(3i)"]=1.to_s
end
@contact = Contact.new(contact_params)
@contact.user = current_user unless current_user.nil?
@contact.save ? (redirect_to contacts_path) : (render :new)
end
def edit
@groups = Group.all
end
def update
@contact.updaters << current_user unless @contact.updaters.include? current_user
if params[:contact][:'birthday(1i)'] == 1900.to_s
params[:contact][:unknown_year] = '1'
else
params[:contact][:unknown_year] = '0'
end
if params[:contact][:'birthday(1i)'] == 1900.to_s && params[:contact][:'birthday(2i)'] == 1.to_s && params[:contact][:'birthday(3i)'] == 1.to_s
params[:contact][:unknown_birthday] = '1'
else
params[:contact][:unknown_birthday] = '0'
end
@contact.update(contact_params) ? (redirect_to contacts_path) : (render :edit)
end
def destroy
@contact.destroy
redirect_to contacts_path
end
def update_month
#http://guides.rubyonrails.org/active_record_querying.html#retrieving-multiple-objects
Contact.find_each(batch_size: 100) do |c|
if c.unknown_birthday
c.update_attribute(:birth_month, 0)
elsif !c.birthday.nil?
c.update_attribute(:birth_month, c.birthday.month)
end
end
redirect_to contacts_path
end
# massively import records
def import
user_info = {:current_user=> current_user, :ip=>my_ip}
begin
file = params[:file]
raise '请选择一个要上传的文件!' unless file.respond_to?(:original_filename)
file_ext = File.extname(file.original_filename)
ci = ContactImporter.new(file.path, extension: file_ext.to_sym, params: user_info)
import_result=ci.import
if ci.row_errors.length == 0 and ci.error_msg.empty?
flash[:notice]= "祝贺你,所有数据都成功导入了!<br>请在联系人列表中逐个编辑用红色背景标记的联系人。<br>检查无误后,请在“审核”一栏打勾,并提交修改。"
else
flash[:error]= ci.error_msg
end
rescue => e
flash[:error]= e.message
#raise
ensure
@contacts = Contact.readonly.where(user: current_user, updated_at: 30.seconds.ago..Time.now ).order('updated_at DESC')
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_contact
@contact = Contact.find(params[:id])
end
def set_birth_month
if @contact.unknown_birthday
@contact.birth_month=0
@contact.save!
elsif !@contact.birthday.nil?
@contact.birth_month=@contact.birthday.month
@contact.save!
end
end
def contact_params
params.require(:contact).permit(
:name,
:gender,
:telephone,
:mobile,
:email,
:wechat,
:address,
:birthday,
:unknown_year,
:unknown_birthday,
:birth_month,
:come,
:decision,
:decision_with,
:baptism,
:go,
:created_at,
:comment,
:job,
:find_us,
:find_us_additional,
:friend_id,
:pray,
:native_place,
:spouse,
:authenticated,
:register_ip,
:q => [],
:participated_group_ids => [],
:participated_gathering_ids => []
)
end
end