Skip to content

Commit

Permalink
Merge pull request #7 from performant-software/feature/udf1_migration
Browse files Browse the repository at this point in the history
UDF #1 - Migration
  • Loading branch information
dleadbetter authored Sep 16, 2022
2 parents dcd2de7 + 9468c0b commit ac0d054
Show file tree
Hide file tree
Showing 20 changed files with 212 additions and 2 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/.bundle/
/doc/
/log/*.log
/pkg/
/tmp/
/test/dummy/db/*.sqlite3
/test/dummy/db/*.sqlite3-*
/test/dummy/log/*.log
/test/dummy/storage/
/test/dummy/tmp/
12 changes: 12 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

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

gem "sqlite3"

gem "sprockets-rails"

# Start debugger with binding.b [https://github.com/ruby/debug]
# gem "debug", ">= 1.0.0"
20 changes: 20 additions & 0 deletions MIT-LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright 2022 Performant Software Solutions, LLC

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
49 changes: 47 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,47 @@
# user-defined-fields
Allow users to decide what data to capture
# UserDefinedFields
This gem is designed to serve and storage and configuration for allowing CMS users to dynamically define fields for their models.

## Installation
Add this line to your application's Gemfile:

```ruby
gem 'user_defined_fields'
```

And then execute:
```bash
$ bundle install
```

Or install it yourself as:
```bash
$ gem install user_defined_fields
```

## Usage

#### Migrations
To install the `user_defined` column on a model, use the following command:

```bash
bundle exec rails generate user_defined_fields:install my_model
```

This will generate the following migration:

```ruby
class InstallUserDefinedFieldsOnMyModel < ActiveRecord::Migration[7.0]
def up
add_column :my_model, :user_defined, :jsonb, default: {}
add_index :my_model, :user_defined, using: :gin
end

def down
remove_index :my_model, :user_defined
remove_column :my_model, :user_defined
end
end
```

## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
8 changes: 8 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "bundler/setup"

APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
load "rails/tasks/engine.rake"

load "rails/tasks/statistics.rake"

require "bundler/gem_tasks"
Empty file added app/controllers/concerns/.keep
Empty file.
4 changes: 4 additions & 0 deletions app/controllers/user_defined_fields/application_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module UserDefinedFields
class ApplicationController < ActionController::API
end
end
4 changes: 4 additions & 0 deletions app/jobs/user_defined_fields/application_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module UserDefinedFields
class ApplicationJob < ActiveJob::Base
end
end
6 changes: 6 additions & 0 deletions app/mailers/user_defined_fields/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module UserDefinedFields
class ApplicationMailer < ActionMailer::Base
default from: "[email protected]"
layout "mailer"
end
end
Empty file added app/models/concerns/.keep
Empty file.
5 changes: 5 additions & 0 deletions app/models/user_defined_fields/application_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module UserDefinedFields
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
end
14 changes: 14 additions & 0 deletions bin/rails
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails gems
# installed from the root of your application.

ENGINE_ROOT = File.expand_path("..", __dir__)
ENGINE_PATH = File.expand_path("../lib/user_defined_fields/engine", __dir__)
APP_PATH = File.expand_path("../test/dummy/config/application", __dir__)

# Set up gems listed in the Gemfile.
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
require "bundler/setup" if File.exist?(ENV["BUNDLE_GEMFILE"])

require "rails/all"
require "rails/engine/commands"
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
UserDefinedFields::Engine.routes.draw do
end
29 changes: 29 additions & 0 deletions lib/generators/user_defined_fields/install_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'rails/generators/active_record'

module UserDefinedFields
module Generators
class InstallGenerator < Rails::Generators::Base
# Includes
include ActiveRecord::Generators::Migration

# Command arguments
argument :model_name, type: :string

# Migration template directory
source_root File.join(__dir__, 'templates')

def copy_migration
migration_template(
'install.rb',
"db/migrate/install_user_defined_fields_on_#{model_name}.rb",
migration_version: migration_version,
model_name: model_name
)
end

def migration_version
"[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]"
end
end
end
end
11 changes: 11 additions & 0 deletions lib/generators/user_defined_fields/templates/install.rb.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version %>
def up
add_column :<%= model_name %>, :user_defined, :jsonb, default: {}
add_index :<%= model_name %>, :user_defined, using: :gin
end

def down
remove_index :<%= model_name %>, :user_defined
remove_column :<%= model_name %>, :user_defined
end
end
4 changes: 4 additions & 0 deletions lib/tasks/user_defined_fields_tasks.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# desc "Explaining what the task does"
# task :user_defined_fields do
# # Task goes here
# end
6 changes: 6 additions & 0 deletions lib/user_defined_fields.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require "user_defined_fields/version"
require "user_defined_fields/engine"

module UserDefinedFields
# Your code goes here...
end
6 changes: 6 additions & 0 deletions lib/user_defined_fields/engine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module UserDefinedFields
class Engine < ::Rails::Engine
isolate_namespace UserDefinedFields
config.generators.api_only = true
end
end
3 changes: 3 additions & 0 deletions lib/user_defined_fields/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module UserDefinedFields
VERSION = "0.1.0"
end
21 changes: 21 additions & 0 deletions user_defined_fields.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require_relative 'lib/user_defined_fields/version'

Gem::Specification.new do |spec|
spec.name = 'user_defined_fields'
spec.version = UserDefinedFields::VERSION
spec.authors = ['Performant Software Solutions']
spec.email = ['[email protected]']
spec.homepage = 'https://github.com/performant-software/user-defined-fields'
spec.summary = 'Metadata made easy.'
spec.description = 'A gem to empower users to define their own metadata.'
spec.license = 'MIT'

spec.metadata['homepage_uri'] = spec.homepage
spec.metadata['source_code_uri'] = 'https://github.com/performant-software/user-defined-fields'

spec.files = Dir.chdir(File.expand_path(__dir__)) do
Dir['{app,config,db,lib}/**/*', 'MIT-LICENSE', 'Rakefile', 'README.md']
end

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

0 comments on commit ac0d054

Please sign in to comment.