rails_taggable 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3e93a7db4018d43e82be6909f40da8d2e13b207f1cc5c5e2004dcaff20d8cfbf
4
+ data.tar.gz: 3a01330c9b19a9317da0a43077d523e5546d5d8ccbf23e9515e2f13147d9227b
5
+ SHA512:
6
+ metadata.gz: e9425e708bf3dc34d0c97d834437acc5ee06e755d729af36ab735a35fca5c4afd668732fac77c5a617d3d1f56aa4637b6d80db978419a3c2164d63a59f27f079
7
+ data.tar.gz: 2a23400c0de14948417895e9e16ace872bc51b0bcd96bb9b1a41df29c5a1adb00b66e1476590572f01c4dc0591027597298125e90188ce0017d08ce06a520414
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2021 afeiship
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,17 @@
1
+ # rails_taggable
2
+ > Rails taggable for multiple models.
3
+
4
+ ## installation
5
+ ```bash
6
+ # add migrations to app/db/migrations directory
7
+ rails g rails_taggable:install
8
+
9
+ # add config/initializers/rails_taggable.rb
10
+ rails g rails_taggable:initializer
11
+ ```
12
+
13
+ ## resources
14
+ - https://stackoverflow.com/questions/4141739/generators-and-migrations-in-plugins-rails-3
15
+ - https://dj-bri-t.net/2012/05/rails-3-generators-adding-migration-templates/
16
+ - https://www.coder.work/article/1097309
17
+ - https://stackoverflow.com/questions/13694654/specifying-column-name-in-a-references-migration
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'RailsTaggable'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ load 'rails/tasks/statistics.rake'
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = false
30
+ end
31
+
32
+ task default: :test
@@ -0,0 +1 @@
1
+ //= link_directory ../stylesheets/rails_taggable .css
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,5 @@
1
+ module RailsTaggable
2
+ class ApplicationController < ActionController::Base
3
+ protect_from_forgery with: :exception
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ module RailsTaggable
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module RailsTaggable
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module RailsTaggable
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: 'from@example.com'
4
+ layout 'mailer'
5
+ end
6
+ end
@@ -0,0 +1,14 @@
1
+ module RailsTaggable
2
+ module Taggable
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ has_many :taggings, :as => :taggable, class_name: 'RailsTaggable::Tagging'
7
+ has_many :tags, :through => :taggings, class_name: 'RailsTaggable::Tag'
8
+ end
9
+
10
+ def tag_names
11
+ tags.map(&:name)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ module RailsTaggable
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,20 @@
1
+ module RailsTaggable
2
+ class Tag < ApplicationRecord
3
+ # filters
4
+ before_save :set_defaults
5
+
6
+ # relationship
7
+ has_many :taggings, dependent: :destroy, class_name: 'RailsTaggable::Tagging'
8
+ RailsTaggable.models.each do |item|
9
+ has_many item.underscore.downcase.pluralize.to_sym, through: :taggings, :source => :taggable, :source_type => item
10
+ end
11
+
12
+ # private
13
+ private def set_defaults
14
+ self.code ||= loop do
15
+ random_code = SecureRandom.hex(10).slice(0, 6)
16
+ break random_code unless self.class.exists?(code: random_code)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,6 @@
1
+ module RailsTaggable
2
+ class Tagging < ApplicationRecord
3
+ belongs_to :tag
4
+ belongs_to :taggable, polymorphic: true
5
+ end
6
+ end
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Rails taggable</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= stylesheet_link_tag "rails_taggable/application", media: "all" %>
9
+ </head>
10
+ <body>
11
+
12
+ <%= yield %>
13
+
14
+ </body>
15
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ RailsTaggable::Engine.routes.draw do
2
+ end
@@ -0,0 +1,10 @@
1
+ class CreateRailsTaggableTags < ActiveRecord::Migration[6.0]
2
+ def change
3
+ create_table :rails_taggable_tags do |t|
4
+ t.string :name
5
+ t.string :code
6
+
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ class CreateRailsTaggableTaggings < ActiveRecord::Migration[6.0]
2
+ def change
3
+ create_table :rails_taggable_taggings do |t|
4
+ t.references :tag, null: false, foreign_key: { to_table: :rails_taggable_tags }
5
+ t.references :taggable, polymorphic: true, null: false
6
+
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ module RailsTaggable
2
+ class InitializerGenerator < Rails::Generators::Base
3
+ source_root File.expand_path("templates", __dir__)
4
+ desc "Initialize necessary files to use RailsTaggable"
5
+
6
+ def create_initializer
7
+ copy_file "initializers/rails_taggable.rb", "config/initializers/rails_taggable.rb"
8
+ puts "Add rails_taggable.rb to: config/initializer/rails_taggable.rb"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ module RailsTaggable
2
+ class InstallGenerator < Rails::Generators::Base
3
+ source_root File.expand_path("templates", __dir__)
4
+ desc "Configure necessary files to use RailsTaggable"
5
+
6
+ def create_migration
7
+ rake "rails_taggable:install:migrations"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ RailsTaggable.setup do |config|
2
+ config.models = [
3
+ # 'Post'
4
+ ]
5
+ end
@@ -0,0 +1,5 @@
1
+ module RailsTaggable
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace RailsTaggable
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module RailsTaggable
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,12 @@
1
+ require "rails_taggable/engine"
2
+
3
+ module RailsTaggable
4
+ self.mattr_accessor :models
5
+ self.models = []
6
+ # add default values of more config vars here
7
+
8
+ # this function maps the vars from your app into your engine
9
+ def self.setup(&block)
10
+ yield self
11
+ end
12
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :rails_taggable do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_taggable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - afeiship
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-12-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 6.0.4
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 6.0.4.4
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 6.0.4
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 6.0.4.4
33
+ - !ruby/object:Gem::Dependency
34
+ name: sqlite3
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ description: Rails taggable gem.
48
+ email:
49
+ - 1290657123@qq.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - MIT-LICENSE
55
+ - README.md
56
+ - Rakefile
57
+ - app/assets/config/rails_taggable_manifest.js
58
+ - app/assets/stylesheets/rails_taggable/application.css
59
+ - app/controllers/rails_taggable/application_controller.rb
60
+ - app/helpers/rails_taggable/application_helper.rb
61
+ - app/jobs/rails_taggable/application_job.rb
62
+ - app/mailers/rails_taggable/application_mailer.rb
63
+ - app/models/concerns/rails_taggable/taggable.rb
64
+ - app/models/rails_taggable/application_record.rb
65
+ - app/models/rails_taggable/tag.rb
66
+ - app/models/rails_taggable/tagging.rb
67
+ - app/views/layouts/rails_taggable/application.html.erb
68
+ - config/routes.rb
69
+ - db/migrate/20211217155310_create_rails_taggable_tags.rb
70
+ - db/migrate/20211217155336_create_rails_taggable_taggings.rb
71
+ - lib/generators/rails_taggable/initializer_generator.rb
72
+ - lib/generators/rails_taggable/install_generator.rb
73
+ - lib/generators/rails_taggable/templates/initializers/rails_taggable.rb
74
+ - lib/rails_taggable.rb
75
+ - lib/rails_taggable/engine.rb
76
+ - lib/rails_taggable/version.rb
77
+ - lib/tasks/rails_taggable_tasks.rake
78
+ homepage: https://github.com/afeiship/rails_taggable
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubygems_version: 3.0.3
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Rails taggable for multiple models.
101
+ test_files: []