Code Monkey home page Code Monkey logo

postgresql_jsonb_ransack_rails_5's Introduction

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:

class Field
  include ActiveModel::Validations
  include ActiveModel::Conversion

  attr_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
  end

  def persisted?
    false
  end

  def validate_key
    validates_length_of :key, maximum: 100
    validates_presence_of :key
  end

  def key_parameterize
    self.key = self.key.parameterize.underscore if self.key.present?
  end

  def as_json
      {
          name: name,
          type: type,
          is_filter_exist: is_filter_exist,
          is_sort_exist: is_sort_exist
      }
  end
end

PeopleController.rb

class PeopleController < ApplicationController

  before_action :set_user
  before_action :set_fields
  before_action :set_person, only: [:show, :edit, :update, :destroy]

  def new
    @person = Person.new
  end

  def 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
  end

  def edit
  end

  def 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
  end

  def show
  end

  def destroy
    @person.destroy
    flash[:success] = 'Person destroyed successfully'
    redirect_to @user
  end

  private

    def set_user
      @user = User.find(params[:user_id])
    end

    def set_person
      @person = Person.find(params[:id])
    end

    def set_fields
      @fields = @user.fields['person']
    end

    def person_params
      params.require(:person).permit(:name, :surname, :age, :email)
    end

    def person_metadata_params
      parameters = @fields.keys
      params.require(:person).permit(parameters).to_hash
    end

end

postgresql_jsonb_ransack_rails_5's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

ktdmedia

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.