From ece374b85ccfa2fbdf1db79e201baef1bf586776 Mon Sep 17 00:00:00 2001 From: dleadbetter <> Date: Wed, 17 Apr 2024 10:27:22 -0400 Subject: [PATCH 1/2] CDC #27 - Adding "resource_api" dependency; Renaming "attribute" to "attribute_name"; Adding fuzzy_date_serializer --- Gemfile | 2 ++ Gemfile.lock | 16 ++++++++++++++++ .../concerns/fuzzy_dates/fuzzy_dateable.rb | 8 ++++---- .../fuzzy_dates/fuzzy_date_serializer.rb | 6 ++++++ ...30901161604_create_fuzzy_dates_fuzzy_dates.rb | 4 ++-- fuzzy_dates.gemspec | 1 + 6 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 app/serializers/fuzzy_dates/fuzzy_date_serializer.rb diff --git a/Gemfile b/Gemfile index 72203de..b098cb1 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/Gemfile.lock b/Gemfile.lock index f1b8679..bd0e396 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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/ @@ -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) @@ -157,6 +172,7 @@ PLATFORMS DEPENDENCIES fuzzy_dates! + resource_api! BUNDLED WITH 2.3.16 diff --git a/app/models/concerns/fuzzy_dates/fuzzy_dateable.rb b/app/models/concerns/fuzzy_dates/fuzzy_dateable.rb index 5a96ce1..bf5fca6 100644 --- a/app/models/concerns/fuzzy_dates/fuzzy_dateable.rb +++ b/app/models/concerns/fuzzy_dates/fuzzy_dateable.rb @@ -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 @@ -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 @@ -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 diff --git a/app/serializers/fuzzy_dates/fuzzy_date_serializer.rb b/app/serializers/fuzzy_dates/fuzzy_date_serializer.rb new file mode 100644 index 0000000..f32fe63 --- /dev/null +++ b/app/serializers/fuzzy_dates/fuzzy_date_serializer.rb @@ -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 \ No newline at end of file diff --git a/db/migrate/20230901161604_create_fuzzy_dates_fuzzy_dates.rb b/db/migrate/20230901161604_create_fuzzy_dates_fuzzy_dates.rb index bf3b12c..dce0182 100644 --- a/db/migrate/20230901161604_create_fuzzy_dates_fuzzy_dates.rb +++ b/db/migrate/20230901161604_create_fuzzy_dates_fuzzy_dates.rb @@ -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 @@ -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 diff --git a/fuzzy_dates.gemspec b/fuzzy_dates.gemspec index 5a64154..86ffcf2 100644 --- a/fuzzy_dates.gemspec +++ b/fuzzy_dates.gemspec @@ -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 From d6e7789416a844e0de2d5b8c2fecbbbe7ae32e34 Mon Sep 17 00:00:00 2001 From: dleadbetter <> Date: Thu, 18 Apr 2024 08:13:29 -0400 Subject: [PATCH 2/2] CDC #27 - Updating README.md with basic instructions --- README.md | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 049070d..b5c78dd 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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).