Skip to content

Commit

Permalink
Merge pull request #1 from performant-software/feature/cdc27_events
Browse files Browse the repository at this point in the history
CDC #27 - Events
  • Loading branch information
dleadbetter authored Apr 18, 2024
2 parents 677f4d0 + d6e7789 commit fa3f237
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

gem 'resource_api', git: 'https://github.com/performant-software/resource-api.git', tag: 'v0.5.8'

# Specify your gem's dependencies in fuzzy_dates.gemspec.
gemspec

Expand Down
16 changes: 16 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
GIT
remote: https://github.com/performant-software/resource-api.git
revision: 4fba94a095260c2ac38cf04d46950b98ae230964
tag: v0.5.8
specs:
resource_api (0.1.0)
pagy (~> 5.10)
pundit (~> 2.3.1)
rails (>= 7.0, < 8)

PATH
remote: .
specs:
fuzzy_dates (0.1.0)
rails (>= 6.0.3.2, < 8)
resource_api

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -109,6 +120,10 @@ GEM
racc (~> 1.4)
nokogiri (1.15.4-x86_64-darwin)
racc (~> 1.4)
pagy (5.10.1)
activesupport
pundit (2.3.1)
activesupport (>= 3.0.0)
racc (1.7.1)
rack (2.2.8)
rack-test (2.1.0)
Expand Down Expand Up @@ -157,6 +172,7 @@ PLATFORMS

DEPENDENCIES
fuzzy_dates!
resource_api!

BUNDLED WITH
2.3.16
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# FuzzyDates
Short description and motivation.
Fuzzy Dates is designed to store date values within a specified amount of accuracy. Each date is stored as a range and a descriptor, so that programmatic operations can still be performed, while providing a user-friendly label.

## Installation
Add this line to your application's Gemfile:
Expand All @@ -19,7 +19,21 @@ $ gem install fuzzy_dates
```

## Usage
How to use my plugin.

```ruby
class MyModel < ApplicationRecord
include FuzzyDates::FuzzyDateable

has_fuzzy_dates :start_date, :end_date
end
```

```ruby
class MyModelsSerializer < BaseSerializer
index_attributes :id, :name, start_date: FuzzyDates::FuzzyDateSerializer, end_date: FuzzyDates::FuzzyDateSerializer
show_attributes :id, :name, start_date: FuzzyDates::FuzzyDateSerializer, end_date: FuzzyDates::FuzzyDateSerializer
end
```

## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
8 changes: 4 additions & 4 deletions app/models/concerns/fuzzy_dates/fuzzy_dateable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ module FuzzyDateable
def self.has_fuzzy_dates(*attributes)
attributes.each do |attribute|
# Relationship
has_one attribute, -> { where(attribute: attribute) }, as: :dateable, class_name: 'FuzzyDate', dependent: :destroy, required: false, inverse_of: :dateable
has_one attribute, -> { where(attribute_name: attribute) }, as: :dateable, class_name: 'FuzzyDates::FuzzyDate', dependent: :destroy, required: false, inverse_of: :dateable

# Nested attributes
accepts_nested_attributes_for attribute, allow_destroy: true, reject_if: :is_empty?

# Resourceable params
allow_params "#{attribute}_attributes".to_sym => [:id, :attribute, :accuracy, :range, :start_date, :end_date, :description, :_destroy]
allow_params "#{attribute}_attributes".to_sym => [:id, :accuracy, :range, :start_date, :end_date, :description, :_destroy]

define_singleton_method("#{attribute}_join".to_sym) do
table = self.arel_table
Expand All @@ -23,7 +23,7 @@ def self.has_fuzzy_dates(*attributes)
.on(
date_table[:dateable_id].eq(table[:id])
.and(date_table[:dateable_type].eq(self.to_s)
.and(date_table[:attribute].eq(attribute)))
.and(date_table[:attribute_name].eq(attribute)))
)
.join_sources
end
Expand All @@ -33,7 +33,7 @@ def self.has_fuzzy_dates(*attributes)
private

def is_empty?(attributes)
!(attributes['start_date'].present? && attributes['end_date'].present?)
!attributes['_destroy'].present? && !(attributes['start_date'].present? && attributes['end_date'].present?)
end

end
Expand Down
6 changes: 6 additions & 0 deletions app/serializers/fuzzy_dates/fuzzy_date_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module FuzzyDates
class FuzzyDateSerializer < BaseSerializer
index_attributes :id, :accuracy, :range, :start_date, :end_date, :description
show_attributes :id, :accuracy, :range, :start_date, :end_date, :description
end
end
4 changes: 2 additions & 2 deletions db/migrate/20230901161604_create_fuzzy_dates_fuzzy_dates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class CreateFuzzyDatesFuzzyDates < ActiveRecord::Migration[7.0]
def change
create_table :fuzzy_dates_fuzzy_dates do |t|
t.references :dateable, polymorphic: true, null: false, index: true
t.string :attribute
t.string :attribute_name
t.integer :accuracy
t.boolean :range
t.date :start_date
Expand All @@ -12,6 +12,6 @@ def change
t.timestamps
end

add_index :fuzzy_dates_fuzzy_dates, [:dateable_id, :dateable_type, :attribute], name: 'index_fuzzy_dates_dateable_id_dateable_type_attribute'
add_index :fuzzy_dates_fuzzy_dates, [:dateable_id, :dateable_type, :attribute_name], name: 'index_fuzzy_dates_dateable_id_dateable_type_attribute_name'
end
end
1 change: 1 addition & 0 deletions fuzzy_dates.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ Gem::Specification.new do |spec|
end

spec.add_dependency 'rails', '>= 6.0.3.2', '< 8'
spec.add_dependency 'resource_api'
end

0 comments on commit fa3f237

Please sign in to comment.