iprog_export_model_to_xlsx 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 16fb880fb6db032ebf95a6af2552846b6c513eca11dde338ca141d0c9605488a
4
- data.tar.gz: 63fbf3246266fe70cdd7828aa263e523274c6f9e04106e85cc9e169ea90a3985
3
+ metadata.gz: '0647085c8ca57f92dab776075b418c802a53e54e8c1963cdce368ed1fc45c323'
4
+ data.tar.gz: 2964cde8dd4ee9810e98bb03fcb33703abdbacf9dad92a17a407189ef3ea1fc3
5
5
  SHA512:
6
- metadata.gz: 4decbe461688515865cef8e4a7e4a1a985465e44b2d3baa42a46d0f561c9d9a58f80b336ac9828b0be73c13d5636972234a7b0cb735e02448db226792aefcab4
7
- data.tar.gz: 46310f025a69bc1eec6cc3292ee9c3f41affa98ed1a4e763d1092e981b357f49f3555c11e98f4ce59844632e505fbc8041444b4e07d55731c8d5da44035b8946
6
+ metadata.gz: f9ab6f3f7b913099c89bd891057029c2a60a6f250bb0a2acafd413d6380898407e9528687912429f53f43757130b62727af8d155a06440c79052916d5d916d3d
7
+ data.tar.gz: 5244e6f1e68cf8031de3cd9cd457be6f57b9b7d0bff8811071721292b25563720bcb3b74fd1b6f6c059067299a18d122f347fa83c3fbf698c5d27b95741d8682
data/README.md CHANGED
@@ -1,9 +1,8 @@
1
- # IprogExportModelToXlsx
2
1
 
3
- Welcome to IprogExportModelToXlsx! This gem provides functionality to export ActiveRecord models to XLSX format.
2
+ # Iprog Export Model ToXlsx
3
+ Welcome to iprog_export_model_to_xlsx! This gem provides functionality to export ActiveRecord models to XLSX format.
4
4
 
5
5
  ## Installation
6
-
7
6
  To install the gem and add it to your application's Gemfile, run:
8
7
 
9
8
  $ bundle add iprog_export_model_to_xlsx
@@ -12,49 +11,128 @@ If you're not using Bundler to manage dependencies, install the gem by running:
12
11
 
13
12
  $ gem install iprog_export_model_to_xlsx
14
13
 
15
- ## Usage
16
-
17
- To use the gem in your project, include it with:
14
+ ## Initialization
15
+ To use the gem in your rails project, include it with:
18
16
 
17
+ # app/models/model.rb
19
18
  require 'iprog_export_model_to_xlsx'
20
-
19
+
20
+ class Model < ApplicationRecord
21
+ extend IprogExportModelToXlsx
22
+ end
23
+
21
24
  You can then export your models to XLSX format as shown below.
22
25
 
23
26
  ### Basic Usage
24
-
25
27
  Export without any options:
26
28
 
27
- Model.export_to_xlsx('models.xlsx')
29
+ # with default filepath
30
+ Model.export_to_xlsx
31
+ Output file: rails_app_folder/models.xlsx
28
32
 
29
- ### Usage with Options
33
+ # with custom filepath
34
+ Model.export_to_xlsx('public/custom_models.xlsx')
35
+ Output file: rails_app_folder/public/models.xlsx
30
36
 
37
+ ### Sample with Options in a Model Class
31
38
  You can customize the export by providing options:
32
-
33
- options = {
34
- exclude_columns: ['created_at', 'updated_at'],
35
- limit: 100
36
- }
39
+
40
+ # adding custom class methods
41
+ require 'iprog_export_model_to_xlsx'
37
42
 
38
- Model.export_to_xlsx('models.xlsx', options)
39
-
40
- ## Development
41
-
42
- After cloning the repository, run `bin/setup` to install dependencies. For an interactive prompt to experiment with the code, run `bin/console`.
43
-
44
- To install the gem locally, use:
45
-
46
- $ bundle exec rake install
47
-
48
- To release a new version, update the version number in `version.rb` and then run:
49
-
50
- $ bundle exec rake release
51
-
52
- This will create a git tag for the version, push the git commits and the tag, and push the `.gem` file to [RubyGems.org](https://rubygems.org).
53
-
43
+ class Model < ApplicationRecord
44
+ extend IprogExportModelToXlsx
45
+
46
+ def self.export_published_items_to_xlsx filepath = nil
47
+
48
+ # Custom condition
49
+ custom_conditions = ->(scope) { scope.where(status: 'published') }
50
+
51
+ # Custom column formats
52
+ column_formats = { 'status' => ->(value) { value.upcase } }
53
+
54
+ # Custom progress callback
55
+ custom_progress_callback = ->(current, total) { puts "Custom Progress: Exported #{current}/#{total} records" }
56
+
57
+ options = {
58
+ exclude_columns: ['created_at', 'updated_at'],
59
+ limit: 100,
60
+ conditions: custom_conditions,
61
+ column_formats: column_formats,
62
+ sheet_name: "Published Items",
63
+ progress_callback: custom_progress_callback
64
+ }
65
+
66
+ # with custom filepath
67
+ export_to_xlsx(filepath, options)
68
+
69
+ # OR
70
+
71
+ # with default filepath
72
+ export_to_xlsx(filepath, options)
73
+ end
74
+ end
75
+
76
+ # Usage with custom file path
77
+ Model.export_published_items_to_xlsx("published_items.xlsx")
78
+ Output: rails_app_folder/published_items.xlsx
79
+
80
+ # Usage with default file path
81
+ Model.export_published_items_to_xlsx
82
+ Output: rails_app_folder/models.xlsx
83
+
84
+ ### Sample in a Service Class
85
+ Create a service class:
86
+
87
+ # app/services/model_export_service.rb
88
+ class ModelExportService
89
+ attr_reader :model, :exclude_columns, :limit, :conditions, :sheet_name, :column_formats, :progress_callback
90
+
91
+ def initialize(model, exclude_columns: [], limit: nil, conditions: nil, sheet_name: nil, column_formats: {}, progress_callback: nil )
92
+ @model = model.constantize
93
+ @exclude_columns = exclude_columns
94
+ @limit = limit
95
+ @conditions = conditions
96
+ @sheet_name = sheet_name
97
+ @column_formats = column_formats
98
+ @progress_callback = progress_callback
99
+ end
100
+
101
+ def export_to_xlsx(file_path = nil)
102
+ options = {
103
+ exclude_columns: exclude_columns,
104
+ limit: limit,
105
+ conditions: conditions,
106
+ sheet_name: sheet_name,
107
+ column_formats: column_formats,
108
+ progress_callback: progress_callback
109
+ }
110
+
111
+ model.export_to_xlsx(file_path, options)
112
+ rescue StandardError => e
113
+ raise IprogExportModelToXlsx::Error, "Failed to export to XLSX: #{e.message}"
114
+ end
115
+ end
116
+
117
+ # Usage
118
+ model_export_service = ModelExportService.new("Model",
119
+ excluded_columns: ["created_at", "updated_at"],
120
+ limit: 100,
121
+ column_formats: { 'status' => ->(value) { value.upcase } },
122
+ progress_callback: ->(current, total) { puts "Custom Progress: Exported #{current}/#{total} records" }
123
+ )
124
+
125
+ # Usage with custom file path
126
+ model_export_service.export_to_xlsx("published_items.xlsx")
127
+ Output: rails_app_folder/published_items.xlsx
128
+
129
+ # Usage with default file path
130
+ model_export_service.export_to_xlsx
131
+ Output: rails_app_folder/models.xlsx
132
+
133
+
54
134
  ## Contributing
55
-
56
135
  Bug reports and pull requests are welcome on GitHub at https://github.com/iprog21/iprog_export_model_to_xlsx.
57
136
 
58
137
  ## License
59
-
60
138
  This gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IprogExportModelToXlsx
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
@@ -6,26 +6,40 @@ require "axlsx"
6
6
  module IprogExportModelToXlsx
7
7
  class Error < StandardError; end
8
8
 
9
- def export(file_path, options = {})
9
+ def export_to_xlsx(file_path = nil, options = {})
10
10
  exclude_columns = options[:exclude_columns] || []
11
- limit = options[:limit]
12
- attributes = attribute_names - exclude_columns
13
- package = Axlsx::Package.new
11
+ limit = options[:limit]
12
+ conditions = options[:conditions] || ->(scope) { scope }
13
+ sheet_name = options[:sheet_name] || self.name
14
+ column_formats = options[:column_formats] || {}
15
+ progress_callback = options[:progress_callback] || default_progress_callback
16
+ filtered_file_path = file_path || "#{self.name.to_s.downcase}s.xlsx"
14
17
 
15
- package.workbook.add_worksheet(name: name) do |sheet|
16
- # Headers
17
- sheet.add_row attributes
18
+ attributes = self.attribute_names - exclude_columns
18
19
 
19
- # Data
20
- scope = all
20
+ workbook = Axlsx::Package.new
21
+ workbook.workbook.add_worksheet(name: sheet_name) do |sheet|
22
+ sheet.add_row(attributes.map(&:upcase))
23
+ scope = self.all
24
+ scope = conditions.call(scope)
21
25
  scope = scope.limit(limit) if limit
22
- scope.each do |record|
23
- sheet.add_row record.attributes.slice(*attributes).values
26
+ total_records = scope.size
27
+ scope.each_with_index do |record, index|
28
+ row_data = record.attributes.slice(*attributes).map do |attr, value|
29
+ column_formats[attr] ? column_formats[attr].call(value) : value
30
+ end
31
+ sheet.add_row(row_data)
32
+ progress_callback.call(index + 1, total_records)
24
33
  end
25
34
  end
26
-
27
- package.serialize(file_path)
35
+ workbook.serialize(filtered_file_path)
28
36
  rescue StandardError => e
29
- rails Iprog::Arde::Error, "Failed to export to XLSX: #{e.message}"
37
+ raise Iprog::Arde::Error, "Failed to export to XLSX: #{e.message}"
38
+ end
39
+
40
+ private
41
+
42
+ def default_progress_callback
43
+ ->(current, total) { puts "Exported #{current}/#{total} records" }
30
44
  end
31
45
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iprog_export_model_to_xlsx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jayson Presto
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-08-04 00:00:00.000000000 Z
11
+ date: 2024-08-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: caxlsx