Created
December 27, 2012 16:58
-
-
Save sposmen/4389853 to your computer and use it in GitHub Desktop.
Single Inheritance Table(SIT) with chronosmodel and closure_tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
filtered_params in Publications_controller are a method that return only important columns of model. In this case Publication as example | |
Basic Gems | |
https://github.com/mceachen/closure_tree | |
https://github.com/ifad/chronomodel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Element < ActiveRecord::Base | |
include ChronoModel::TimeMachine | |
attr_accessible :title, :specs, :position, :parent_id | |
acts_as_tree :name_column => 'title', :dependent => :destroy, :order => 'sort_order' | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Publication < Element | |
attr_accessible :max_wordcount, :user_ids | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PublicationsController < ApplicationController | |
def create | |
@publication = Publication.new(filtered_params) | |
if @publication.save | |
render json: @publication, status: :created, location: @publication | |
else | |
render json: @publication.errors, status: :conflict | |
end | |
end | |
def show | |
@publication = Publication.find(params[:id]) | |
@publication.history.all | |
render json: @publication | |
end | |
def update | |
@publication = Publication.find(params[:id]) | |
if @publication.update_attributes filtered_params | |
render json: @publication, status: :ok | |
else | |
render json: @publication.errors, status: :conflict | |
end | |
end | |
def destroy | |
@publication = Publication.find(params[:id]) | |
@publication.destroy | |
head :no_content | |
end | |
def filtered_params | |
filtered = {} | |
keys = model(true).accessible_attributes | |
params[model_underscore].each do |key, value| | |
filtered[key.to_sym] = value if keys.include?(key.to_sym) | |
end | |
filtered | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: UTF-8 | |
# This file is auto-generated from the current state of the database. Instead | |
# of editing this file, please use the migrations feature of Active Record to | |
# incrementally modify your database, and then regenerate this schema definition. | |
# | |
# Note that this schema.rb definition is the authoritative source for your | |
# database schema. If you need to create the application database on another | |
# system, you should be using db:schema:load, not running all the migrations | |
# from scratch. The latter is a flawed and unsustainable approach (the more migrations | |
# you'll amass, the slower it'll run and the greater likelihood for issues). | |
# | |
# It's strongly recommended to check this file into your version control system. | |
ActiveRecord::Schema.define(:version => 20121226190809) do | |
create_table "element_hierarchies", :primary_key => "__chrono_id", :force => true, :temporal => true do |t| | |
t.integer "ancestor_id", :null => false | |
t.integer "descendant_id", :null => false | |
t.integer "generations", :null => false | |
end | |
add_index "element_hierarchies", ["ancestor_id", "descendant_id"], :name => "index_element_hierarchies_on_ancestor_id_and_descendant_id", :unique => true | |
add_index "element_hierarchies", ["descendant_id"], :name => "index_element_hierarchies_on_descendant_id" | |
create_table "elements", :force => true, :temporal => true do |t| | |
t.string "title", :default => "" | |
t.text "specs", :default => "" | |
t.string "type", :default => "" | |
t.integer "parent_id" | |
t.datetime "created_at", :null => false | |
t.datetime "updated_at", :null => false | |
t.integer "status", :default => 0 | |
t.text "text", :default => "" | |
t.integer "sort_order", :default => 0 | |
t.integer "project_id" | |
t.string "rationale", :default => "" | |
t.string "standards", :default => "" | |
t.string "dok", :default => "" | |
t.string "rnr", :default => "" | |
t.string "copyspace", :default => "" | |
t.string "blanks", :default => "" | |
t.boolean "is_correct", :default => false | |
t.integer "asset_id" | |
t.string "size", :default => "" | |
t.text "caption", :default => "" | |
t.text "labels", :default => "" | |
t.text "source", :default => "" | |
t.text "original_data", :default => "" | |
t.integer "max_wordcount" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment