diff --git a/Gemfile b/Gemfile index f2479d5..efb60a0 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,10 @@ gem "sequel" gem "sqlite3" group :dev do + # Manually adding IRB as a dependency so I can adjust colors until + # https://github.com/ruby/reline/pull/552 is in Ruby itself + gem "irb" + gem "minitest" gem "rake" end diff --git a/Gemfile.lock b/Gemfile.lock index 6a5c349..121e87a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,16 +2,28 @@ GEM remote: https://rubygems.org/ specs: bigdecimal (3.1.4) + io-console (0.6.0) + irb (1.9.0) + rdoc + reline (>= 0.3.8) minitest (5.20.0) + psych (5.1.1.1) + stringio rake (13.0.6) + rdoc (6.6.0) + psych (>= 4.0.0) + reline (0.4.0) + io-console (~> 0.5) sequel (5.73.0) bigdecimal sqlite3 (1.6.8-arm64-darwin) + stringio (3.0.9) PLATFORMS arm64-darwin-22 DEPENDENCIES + irb minitest rake sequel diff --git a/db/migrations/007_add_id_to_models.rb b/db/migrations/007_add_id_to_models.rb new file mode 100644 index 0000000..15be742 --- /dev/null +++ b/db/migrations/007_add_id_to_models.rb @@ -0,0 +1,15 @@ +Sequel.migration do + change do + alter_table(:notes) do + add_primary_key :id + end + + alter_table(:sources) do + add_primary_key :id + end + + alter_table(:titles) do + add_primary_key :id + end + end +end diff --git a/lib/alphadex.rb b/lib/alphadex.rb index 408a7de..a8df2a1 100644 --- a/lib/alphadex.rb +++ b/lib/alphadex.rb @@ -1,13 +1,13 @@ require_relative "../models" module Alphadex - def self.add_bookmark(url, title, note, tags) + def self.add_bookmark(url:, title:, note:, tags:) fail "url already exists" unless Url.where(url:).empty? DB.transaction do entity = Entity.create entity.url = Url.new(url:) - entity.title = Title.new(title:) + entity.title = Title.new(title:) if title entity.add_note(text: note) tags.map { Tag.find_or_create(name: _1) }.each do |tag| entity.add_tag(tag) diff --git a/models.rb b/models.rb index 2f1c217..33b76ed 100644 --- a/models.rb +++ b/models.rb @@ -4,35 +4,38 @@ require_relative "db" Sequel::Model.plugin :timestamps -class Entity < Sequel::Model - one_to_many :notes - one_to_many :sources - one_to_one :title - many_to_many :tags - one_to_one :url - - def add_tag(tag) - DB[:entities_tags].insert(entity_id: self.id, tag_id: tag.id, created_at: DateTime.now) +module Alphadex + class Entity < Sequel::Model + one_to_many :notes + one_to_many :sources + one_to_one :title + many_to_many :tags + one_to_one :url + + # override this since I have `created_at` on this join table for some reason + def add_tag(tag) + DB[:entities_tags].insert(entity_id: self.id, tag_id: tag.id, created_at: DateTime.now) + end end -end -class Note < Sequel::Model - many_to_one :entity -end + class Note < Sequel::Model + many_to_one :entity + end -class Source < Sequel::Model - many_to_one :entity - many_to_one :source, class: :Entity -end + class Source < Sequel::Model + many_to_one :entity + many_to_one :source, class: :Entity + end -class Title < Sequel::Model - many_to_one :entity -end + class Title < Sequel::Model + many_to_one :entity + end -class Tag < Sequel::Model - many_to_many :entities -end + class Tag < Sequel::Model + many_to_many :entities + end -class Url < Sequel::Model - one_to_one :entity + class Url < Sequel::Model + many_to_one :entity + end end