Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lab2023/postgresql_jsonb_ransack_rails_5
Rails user based custom field example with postgresql jsonb and using custom fields with ransack
https://github.com/lab2023/postgresql_jsonb_ransack_rails_5
custom-fields jsonb postgresql rails rails5 ransack
Last synced: 1 day ago
JSON representation
Rails user based custom field example with postgresql jsonb and using custom fields with ransack
- Host: GitHub
- URL: https://github.com/lab2023/postgresql_jsonb_ransack_rails_5
- Owner: lab2023
- Created: 2017-04-17T13:25:30.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-04-17T13:25:48.000Z (over 7 years ago)
- Last Synced: 2024-10-29T14:15:25.769Z (10 days ago)
- Topics: custom-fields, jsonb, postgresql, rails, rails5, ransack
- Language: Ruby
- Size: 43 KB
- Stars: 10
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Rails user based custom field example with postgresql jsonb and using custom fields with ransack
Demo: https://rails-custom-field-ransack.herokuapp.com
This example project for using custom field in rails. You can add new fields, and new resources related with your fields.
Virtual attribute for custom field:
```ruby
class Field
include ActiveModel::Validations
include ActiveModel::Conversionattr_accessor :name, :type, :key, :is_filter_exist, :is_sort_exist
validates_presence_of :name, :type
validates_inclusion_of :type, in: %w(number text)def initialize(attributes = {})
self.is_filter_exist = false
self.is_sort_exist = false
attributes.each do |name, value|
if name.in?(%w(is_filter_exist is_sort_exist))
value = value.to_s.in?( %w(1 true) )
end
send("#{name}=", value)
end
enddef persisted?
false
enddef validate_key
validates_length_of :key, maximum: 100
validates_presence_of :key
enddef key_parameterize
self.key = self.key.parameterize.underscore if self.key.present?
enddef as_json
{
name: name,
type: type,
is_filter_exist: is_filter_exist,
is_sort_exist: is_sort_exist
}
end
end
```PeopleController.rb
```ruby
class PeopleController < ApplicationControllerbefore_action :set_user
before_action :set_fields
before_action :set_person, only: [:show, :edit, :update, :destroy]def new
@person = Person.new
enddef create
@person = Person.new(person_params)
@person.metadata = person_metadata_params
if @person.save
flash[:success] = 'Person created successfully'
redirect_to user_person_path(@user, @person)
else
flash.now[:danger] = 'Invalid person parameters!'
render :new
end
enddef edit
enddef update
@person.metadata = person_metadata_params
if @person.update(person_params)
flash[:success] = 'Person updated successfully'
redirect_to user_person_path(@user, @person)
else
flash.now[:danger] = 'Invalid person parameters!'
render :edit
end
enddef show
enddef destroy
@person.destroy
flash[:success] = 'Person destroyed successfully'
redirect_to @user
endprivate
def set_user
@user = User.find(params[:user_id])
enddef set_person
@person = Person.find(params[:id])
enddef set_fields
@fields = @user.fields['person']
enddef person_params
params.require(:person).permit(:name, :surname, :age, :email)
enddef person_metadata_params
parameters = @fields.keys
params.require(:person).permit(parameters).to_hash
endend
```