Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

iprog_export_model_to_xlsx

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

iprog_export_model_to_xlsx

  • 0.1.2
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Iprog Export Model ToXlsx

Welcome to iprog_export_model_to_xlsx! This gem provides functionality to export ActiveRecord models to XLSX format.

Installation

To install the gem and add it to your application's Gemfile, run:

$ bundle add iprog_export_model_to_xlsx

If you're not using Bundler to manage dependencies, install the gem by running:

$ gem install iprog_export_model_to_xlsx

Initialization

To use the gem in your rails project, include it with:

# app/models/model.rb
require 'iprog_export_model_to_xlsx'

class Model < ApplicationRecord
 extend IprogExportModelToXlsx
end

You can then export your models to XLSX format as shown below.

Basic Usage

Export without any options:

# with default filepath
Model.export_to_xlsx 
 Output file: rails_app_folder/models.xlsx

# with custom filepath
Model.export_to_xlsx('public/custom_models.xlsx')
 Output file: rails_app_folder/public/models.xlsx

Sample with Options in a Model Class

You can customize the export by providing options:

# adding custom class methods
require 'iprog_export_model_to_xlsx'

class Model < ApplicationRecord
 extend IprogExportModelToXlsx
 
 def self.export_published_items_to_xlsx filepath = nil
  
  # Custom condition
  custom_conditions = ->(scope) { scope.where(status: 'published') }

  # Custom column formats
  column_formats = { 'status' => ->(value) { value.upcase } }

  # Custom progress callback
  custom_progress_callback = ->(current, total) { puts "Custom Progress: Exported #{current}/#{total} records" } 

  options = {
   exclude_columns: ['created_at', 'updated_at'],
   limit: 100,
   conditions: custom_conditions,
   column_formats: column_formats,
   sheet_name: "Published Items",
   progress_callback: custom_progress_callback
  }

  # with custom filepath
  export_to_xlsx(filepath, options)
    
  # OR
   
  # with default filepath
  export_to_xlsx(filepath, options)
 end
end

# Usage with custom file path
 Model.export_published_items_to_xlsx("published_items.xlsx")
  Output: rails_app_folder/published_items.xlsx
 
# Usage with default file path
 Model.export_published_items_to_xlsx
  Output: rails_app_folder/models.xlsx

Sample in a Service Class

Create a service class:

 # app/services/model_export_service.rb
 class ModelExportService
  attr_reader :model, :exclude_columns, :limit, :conditions, :sheet_name, :column_formats, :progress_callback
  
  def initialize(model, exclude_columns: [], limit: nil, conditions:  nil, sheet_name: nil, column_formats: {}, progress_callback: nil )
   @model             = model.constantize
   @exclude_columns   = exclude_columns
   @limit             = limit
   @conditions        = conditions
   @sheet_name        = sheet_name
   @column_formats    = column_formats
   @progress_callback = progress_callback
  end
  
  def export_to_xlsx(file_path = nil)
   options = {
    exclude_columns: exclude_columns,
    limit: limit,
    conditions: conditions,
    sheet_name: sheet_name,
    column_formats: column_formats,
    progress_callback: progress_callback
   }
   
   model.export_to_xlsx(file_path, options)
  rescue StandardError => e
   raise IprogExportModelToXlsx::Error, "Failed to export to XLSX: #{e.message}"
  end
 end
 
 # Usage
 model_export_service = ModelExportService.new("Model",
   excluded_columns: ["created_at", "updated_at"], 
   limit: 100, 
   column_formats:  { 'status' => ->(value) { value.upcase } }, 
   progress_callback: ->(current, total) { puts "Custom Progress: Exported #{current}/#{total} records" }
 )
 
 # Usage with custom file path
  model_export_service.export_to_xlsx("published_items.xlsx")
   Output: rails_app_folder/published_items.xlsx
 
 # Usage with default file path
  model_export_service.export_to_xlsx
   Output: rails_app_folder/models.xlsx
 

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/iprog21/iprog_export_model_to_xlsx.

License

This gem is available as open source under the terms of the MIT License.

FAQs

Package last updated on 05 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc