Coder Social home page Coder Social logo

jamgar / action-draft Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rails-engine/action-draft

0.0 1.0 0.0 65 KB

✏️Action Draft brings your ActiveRecord model to storage multiple draft attributes without add columns to the business table.

License: MIT License

Ruby 84.91% JavaScript 1.75% CSS 1.57% HTML 11.78%

action-draft's Introduction

Action Draft

Action Draft brings your ActiveRecord model to storage multiple draft attributes without add columns to the business table.

让你的 ActiveRecord Model 能轻易的支持草稿存储功能,而无需在业务表里面增加字段。

Gem Version Build Status

Features

  • Save drafts without add columns to the business table.
  • Work any ActiveRecord model, just add has_draft :field_name.
  • A apply_draft method for assignment the draft values to actual attributes.
  • Fallback to actual attribute value when draft is nil.

Installation

gem "action-draft"

And then execute:

$ bundle
$ rails action_draft:install:migrations

Usage

In your ActiveRecord model:

# app/models/message.rb
class Message < ApplicationRecord
  has_draft :title, :content
end

Now you have draft_title, draft_content attributes.

Then refer to this field in the form for the model:

<%# app/views/messages/_form.html.erb %>
<%= form_with(model: message) do |form| %><div class="field">
    <%= form.label :draft_title %>
    <%= form.textarea :draft_title %>
  </div>

  <div class="field">
    <%= form.label :draft_content %>
    <%= form.textarea :draft_content %>
  </div><% end %>

In your controller

class MessagesController < ApplicationController
  def new
    @message = Message.new
  end

  def create
    @message = Message.new(message_params)
    message.apply_draft if message_params[:publish]
    if message.save
      redirect_to messages_path, notice: "Message has created successfully"
    else
      render :new
    end
  end

  def update
    @message.assign_attributes(message_params)
    message.apply_draft if message_params[:publish]
    if message.save
      redirect_to messages_path, notice: "Message has updated successfully"
    else
      render :edit
    end
  end

  private
    def set_message
      @message = Message.find(params[:id])
    end

    def message_params
      params.require(:message).perrmit(:draft_title, :draft_content, :publish)
    end
end

Save draft attributes:

irb> message = Message.new
irb> message.draft_title = "Draft title"
irb> message.draft_title.to_s
"Draft title"
irb> message.draft_content = "Draft message content"
irb> message.draft_content.to_s
"Draft message content"
irb> message.save

irb> message.reload
irb> message.draft_title.to_s
"Draft title"
irb> message.draft_content.to_s
"Draft message content"

Apply draft content:

irb> message = Message.new
irb> message.draft_title = "Message title"
irb> message.apply_draft

irb> message.title
"Message title"
irb> message.draft_title
"Message title"
irb> message.save

License

The gem is available as open source under the terms of the MIT License.

action-draft's People

Contributors

huacnlee avatar griffinqiu avatar

Watchers

James Cloos avatar

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.