reportable_rails 0.1.1 → 0.1.3
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 +4 -4
- data/README.md +105 -2
- data/lib/reportable_rails/models/report_category.rb +22 -0
- data/lib/reportable_rails/version.rb +1 -1
- data/lib/reportable_rails.rb +5 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ec42191250593fe984e7f62ffb0dfbb6367aaa63226267d687f285141a76a5bf
|
4
|
+
data.tar.gz: 65d2a6a9a3e1c6c7e57343586018daba7257e97cd565bfee87023d79fe48935c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b5cdf9b9698e1f620d19cd18680cef743b6fca62b875cf0c55d313ea893e35e0157a6f013e4f789d12010dca93b26676ca7cf270dd07fb09800ed8d52dbe58a5
|
7
|
+
data.tar.gz: ba76a3b406fbe56b6032c4ed6ee313b445ef4bc6c8d4f9d8f26625201df3c61312d5fbe241e223d17d81f67fceb988e900083f1496d0f0ffd6200880ffa7456c
|
data/README.md
CHANGED
@@ -31,6 +31,9 @@ ReportableRails.configure do |config|
|
|
31
31
|
# Your custom logic here
|
32
32
|
# For example: NotificationMailer.report_submitted(report).deliver_later
|
33
33
|
}
|
34
|
+
|
35
|
+
# Customize the name of the default category
|
36
|
+
config.default_category_name = 'General' # Defaults to 'Uncategorized'
|
34
37
|
end
|
35
38
|
```
|
36
39
|
|
@@ -38,18 +41,47 @@ end
|
|
38
41
|
|
39
42
|
### Setting Up Models
|
40
43
|
|
41
|
-
|
44
|
+
ReportableRails provides three main models that can be included in your application:
|
42
45
|
|
43
46
|
```ruby
|
47
|
+
# app/models/time_report.rb
|
44
48
|
class TimeReport < ApplicationRecord
|
45
49
|
include ReportableRails::Models::Report
|
46
50
|
end
|
51
|
+
|
52
|
+
# app/models/report_category.rb
|
53
|
+
class ReportCategory < ApplicationRecord
|
54
|
+
include ReportableRails::Models::ReportCategory
|
55
|
+
end
|
56
|
+
|
57
|
+
# app/models/hours_log.rb
|
58
|
+
class HoursLog < ApplicationRecord
|
59
|
+
include ReportableRails::Models::HoursLog
|
60
|
+
end
|
47
61
|
```
|
48
62
|
|
49
|
-
|
63
|
+
Each model provides the following functionality:
|
64
|
+
|
65
|
+
#### Report Model
|
50
66
|
- `belongs_to :owner` (user who owns the report)
|
51
67
|
- `belongs_to :report_category` (optional categorization)
|
52
68
|
- `has_many :hours_logs` (time entries for the report)
|
69
|
+
- Period management methods
|
70
|
+
- Report submission handling
|
71
|
+
|
72
|
+
#### Report Category Model
|
73
|
+
- `has_many :reports`
|
74
|
+
- Name and description tracking
|
75
|
+
- Active/inactive status management
|
76
|
+
- Scopes for filtering active categories
|
77
|
+
- Helper methods for finding and managing categories
|
78
|
+
|
79
|
+
#### Hours Log Model
|
80
|
+
- `belongs_to :report`
|
81
|
+
- Hours and date tracking
|
82
|
+
- Period validation
|
83
|
+
- Description management
|
84
|
+
- Methods for checking if hours are in current period
|
53
85
|
|
54
86
|
### Managing Hours Logs
|
55
87
|
|
@@ -92,6 +124,77 @@ report = TimeReport.create!(
|
|
92
124
|
)
|
93
125
|
```
|
94
126
|
|
127
|
+
### Default Category
|
128
|
+
|
129
|
+
ReportableRails automatically manages a default "Uncategorized" category that cannot be deactivated or deleted. You can customize the name of this category in your configuration:
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
ReportableRails.configure do |config|
|
133
|
+
config.default_category_name = 'General' # Defaults to 'Uncategorized'
|
134
|
+
end
|
135
|
+
```
|
136
|
+
|
137
|
+
To access the default category:
|
138
|
+
|
139
|
+
```ruby
|
140
|
+
default_category = ReportCategory.default_category # Returns or creates the default category
|
141
|
+
```
|
142
|
+
|
143
|
+
Reports without a specific category will use this default category. The default category:
|
144
|
+
- Cannot be deactivated
|
145
|
+
- Cannot be deleted
|
146
|
+
- Is automatically created when first accessed
|
147
|
+
|
148
|
+
### Database Setup
|
149
|
+
|
150
|
+
You'll need to create the necessary database tables. Here are the recommended migrations:
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
# Create Reports Table
|
154
|
+
class CreateReports < ActiveRecord::Migration[6.1]
|
155
|
+
def change
|
156
|
+
create_table :reports do |t|
|
157
|
+
t.references :owner, null: false, foreign_key: { to_table: :users }
|
158
|
+
t.references :report_category, foreign_key: true
|
159
|
+
t.timestamps
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
# Create Report Categories Table
|
165
|
+
class CreateReportCategories < ActiveRecord::Migration[6.1]
|
166
|
+
def change
|
167
|
+
create_table :report_categories do |t|
|
168
|
+
t.string :name, null: false
|
169
|
+
t.text :description
|
170
|
+
t.boolean :active, default: true
|
171
|
+
t.timestamps
|
172
|
+
t.index :name, unique: true
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
# Create Hours Logs Table
|
178
|
+
class CreateHoursLogs < ActiveRecord::Migration[6.1]
|
179
|
+
def change
|
180
|
+
create_table :hours_logs do |t|
|
181
|
+
t.references :report, null: false, foreign_key: true
|
182
|
+
t.decimal :hours, null: false, precision: 4, scale: 2
|
183
|
+
t.date :date, null: false
|
184
|
+
t.text :description, null: false
|
185
|
+
t.timestamps
|
186
|
+
t.index [:report_id, :date]
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
```
|
191
|
+
|
192
|
+
Run the migrations with:
|
193
|
+
|
194
|
+
```bash
|
195
|
+
$ rails db:migrate
|
196
|
+
```
|
197
|
+
|
95
198
|
## License
|
96
199
|
|
97
200
|
The gem is available as open source under the terms of the MIT License.
|
@@ -9,18 +9,33 @@ module ReportableRails
|
|
9
9
|
validates :name, presence: true, uniqueness: { case_sensitive: false }
|
10
10
|
validates :description, length: { maximum: 1000 }
|
11
11
|
validates :active, inclusion: { in: [true, false] }
|
12
|
+
validate :prevent_deactivating_default_category
|
12
13
|
|
13
14
|
# Scopes
|
14
15
|
scope :active, -> { where(active: true) }
|
15
16
|
scope :ordered, -> { order(:name) }
|
16
17
|
|
17
18
|
before_validation :set_default_active
|
19
|
+
before_destroy :prevent_destroying_default_category
|
18
20
|
|
19
21
|
private
|
20
22
|
|
21
23
|
def set_default_active
|
22
24
|
self.active = true if active.nil?
|
23
25
|
end
|
26
|
+
|
27
|
+
def prevent_deactivating_default_category
|
28
|
+
if name == ReportableRails.configuration.default_category_name && !active
|
29
|
+
errors.add(:active, "cannot deactivate the default category")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def prevent_destroying_default_category
|
34
|
+
if name == ReportableRails.configuration.default_category_name
|
35
|
+
errors.add(:base, "cannot delete the default category")
|
36
|
+
throw :abort
|
37
|
+
end
|
38
|
+
end
|
24
39
|
end
|
25
40
|
|
26
41
|
module ClassMethods
|
@@ -31,8 +46,15 @@ module ReportableRails
|
|
31
46
|
end
|
32
47
|
end
|
33
48
|
|
49
|
+
# Get or create the default category
|
50
|
+
def default_category
|
51
|
+
find_or_create_by_name(ReportableRails.configuration.default_category_name,
|
52
|
+
description: 'Default category for uncategorized reports')
|
53
|
+
end
|
54
|
+
|
34
55
|
# Deactivate a category without deleting it
|
35
56
|
def deactivate(name)
|
57
|
+
return false if name == ReportableRails.configuration.default_category_name
|
36
58
|
category = find_by(name: name)
|
37
59
|
category&.update(active: false)
|
38
60
|
end
|
data/lib/reportable_rails.rb
CHANGED
@@ -3,6 +3,9 @@
|
|
3
3
|
require 'rails/engine'
|
4
4
|
require 'active_model_serializers'
|
5
5
|
require 'reportable_rails/version'
|
6
|
+
require 'reportable_rails/models/report'
|
7
|
+
require 'reportable_rails/models/report_category'
|
8
|
+
require 'reportable_rails/models/hours_log'
|
6
9
|
|
7
10
|
module ReportableRails
|
8
11
|
class Error < StandardError; end
|
@@ -32,11 +35,12 @@ module ReportableRails
|
|
32
35
|
end
|
33
36
|
|
34
37
|
class Configuration
|
35
|
-
attr_accessor :user_class, :default_category_name
|
38
|
+
attr_accessor :user_class, :default_category_name, :report_submitted_callback
|
36
39
|
|
37
40
|
def initialize
|
38
41
|
@user_class = 'User'
|
39
42
|
@default_category_name = 'Uncategorized'
|
43
|
+
@report_submitted_callback = nil
|
40
44
|
end
|
41
45
|
end
|
42
46
|
end
|