データファイルをシードし、ユーザーが圧縮してエクスポートするさまざまなCSVにデータを追加することを唯一の目的とするアプリがあります。私のアプリケーションコントローラーは、すべて次のような行でいっぱいです。
def export_tips
@appointments = Appointment.order('service_id')
send_data @appointments.to_csv_tips, filename: 'tips.csv'
end
def export_ticketpayments
@appointments = Appointment.order('service_id')
send_data @appointments.to_csv_ticketpayments, filename: 'ticketspaymentitems.csv'
end
def export_batchmanifest
@batchmanifests = Batchmanifest.all
send_data @batchmanifests.to_csv_batchmanifest, filename: "batch_manifest-#{Date.today}.csv"
end
def export_pets
@clients = Client.all
send_data @clients.to_csv_pets, filename: 'pets.csv'
end
def export_clients
@clients = Client.all
send_data @clients.to_csv_clients, filename: 'clients.csv'
end
単一のCSVエクスポートの作成や、複数のzipとCSVを含む複雑なzipファイルの作成など、複数の異なる領域で使用したため、アプリケーションコントローラーにあります。
私がコードをクリーンアップしようとしたいくつかのことは次のとおりです。
私のアプリケーションレコードは、CSVをエクスポートすることを単に意図した繰り返し行で同じように悪いです:
def self.to_csv_appointments
attributes = %w[appointment_id location_id employee_id client_id child_id notes
has_specific_employee start_time end_time]
CSV.generate(headers: true) do |csv|
csv << attributes
all.each do |appointment|
csv << attributes.map { |attr| appointment.send(attr) }
end
end
end
def self.to_csv_appointmentservices
attributes = %w[appointment_id service_id price duration]
CSV.generate(headers: true) do |csv|
csv << attributes
all.each do |appointment|
csv << attributes.map { |attr| appointment.send(attr) }
end
end
end
def self.to_csv_tickets
attributes = %w[ticket_id location_id client_id ticket_status employee_id
employee_id start_time]
headers = %w[ticket_id location_id client_id status employee_id
closed_by_employee_id closed_at]
CSV.generate(headers: true) do |csv|
csv << headers
all.each do |appointment|
csv << attributes.map { |attr| appointment.send(attr) }
end
end
end
アプリケーションレコードについては、アプリケーションコントローラーにリストされている方法と同様の方法を試しましたが、役に立ちませんでした。繰り返しになりますが、サイトの複数の部分でこれらにアクセスする必要があるため、個々のモデルファイルではなくアプリケーションレコードのコードを使用します。
アプリケーションコントローラーからのコードは、主に静的コントローラーとビューファイルのボタンで使用されます。以下に示すように、ファイルセットを作成する機能が必要ですが、ユーザーが1つのCSVのみをエクスポートできるようにする必要もあります。
静的コントローラーからzipファイルの作成までの例:
def create_appointments_zip
file_stream = Zip::OutputStream.write_buffer do |zip|
@appointments = Appointment.order('service_id')
zip.put_next_entry "appointment_manifest.csv"; zip << File.binread("#{Rails.root}/app/assets/csvs/appointment_manifest.csv")
zip.put_next_entry "appointments.csv"; zip << @appointments.to_csv_appointments
zip.put_next_entry "appointment_services.csv"; zip << @appointments.to_csv_appointmentservices
zip.put_next_entry "appointment_statuses.csv"; zip << @appointments.to_csv_appointmentstatuses
end
file_stream.rewind
File.open("#{Rails.root}/app/assets/csvs/appointments.zip", 'wb') do |file|
file.write(file_stream.read)
end
end
def export_salonset
create_appointments_zip
create_tickets_zip
create_inventory_zip
create_memberships_zip
file_stream = Zip::OutputStream.write_buffer do |zip|
@saloncategories = Saloncategory.all
@salonservices = Salonservice.all
@clients = Client.all
@locations = Location.all
@salonpricings = Salonpricing.all
@staffs = Staff.order("location_id")
zip.put_next_entry "batch_manifest.csv"; zip << File.binread("#{Rails.root}/app/assets/csvs/batch_manifest_simple_salon.csv")
zip.put_next_entry "categories.csv"; zip << @saloncategories.to_csv_saloncategories
zip.put_next_entry "clients.csv"; zip << @clients.to_csv_clients
zip.put_next_entry "employees.csv"; zip << @staffs.to_csv_staff
zip.put_next_entry "locations.csv"; zip << @locations.to_csv_locations
zip.put_next_entry "pricings.csv"; zip << @salonpricings.to_csv_pricings
zip.put_next_entry "services.csv"; zip << @salonservices.to_csv_salonservices
zip.put_next_entry "appointments.zip"; zip << File.binread("#{Rails.root}/app/assets/csvs/appointments.zip")
zip.put_next_entry "inventories.zip"; zip << File.binread("#{Rails.root}/app/assets/csvs/inventories.zip")
zip.put_next_entry "tickets.zip"; zip << File.binread("#{Rails.root}/app/assets/csvs/tickets.zip")
zip.put_next_entry "addonmappings.csv"; zip << File.binread("#{Rails.root}/app/assets/csvs/addonmappings.csv")
end
file_stream.rewind
respond_to do |format|
format.zip do
send_data file_stream.read, filename: "salon_set.zip"
end
end
file_stream.rewind
File.open("#{Rails.root}/app/assets/csvs/salon_set.zip", 'wb') do |file|
file.write(file_stream.read)
end
end
役立つ場合は、リポジトリにリンクしますhttps://github.com/atayl16/data-wizard/blob/master/app/controllers/application_controller.rb https://github.com/atayl16/data-wizard/blob/ master / app / models / application_record.rb
これらの同じ行を何度も書くよりも良い方法があるに違いないことを私は知っています。コードは機能し、私のサイトは(驚くほど)機能しますが、経験豊富な開発者が笑わずにリポジトリを見るのは恥ずかしいことです。どんな助けでも大歓迎です!
この目的で、私はこれをクリーンアップするためにメタプログラミングを使用することになりました。簡潔にするために、配列からいくつかの項目を除外した例を次に示します。
["bundle", "attendee", "location", "membership", "client", "staff"].each do |new_method|
define_method("#{new_method.pluralize}") do
instance_variable_set("@#{new_method.pluralize}", new_method.camelcase.constantize.all)
instance_var = instance_variable_get("@#{new_method.pluralize}")
send_data instance_var.public_send("to_csv_#{new_method.pluralize}"), filename: "#{new_method.pluralize}.csv"
end
end
新しく作成したエクスポートコントローラーから30個のメソッドを削除することができました。変更をプッシュした後のコードは次のとおりですhttps://github.com/atayl16/data-wizard/blob/0011b6cf8c1fe967d73a569fa573cedc52cb8c72/app/controllers/export_controller.rb
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加