Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rails-engine/action-draft
✏️ Action Draft brings your ActiveRecord model to storage multiple draft attributes without add columns to the business table.
https://github.com/rails-engine/action-draft
activerecord draft plugin rails
Last synced: 2 months ago
JSON representation
✏️ Action Draft brings your ActiveRecord model to storage multiple draft attributes without add columns to the business table.
- Host: GitHub
- URL: https://github.com/rails-engine/action-draft
- Owner: rails-engine
- License: mit
- Created: 2019-07-24T11:29:53.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2022-03-21T02:53:26.000Z (almost 3 years ago)
- Last Synced: 2024-11-07T17:17:35.295Z (2 months ago)
- Topics: activerecord, draft, plugin, rails
- Language: Ruby
- Homepage:
- Size: 47.9 KB
- Stars: 19
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: MIT-LICENSE
Awesome Lists containing this project
README
# Action Draft
Action Draft brings your ActiveRecord model to storage multiple draft attributes without add columns to the business table.
让你的 ActiveRecord Model 能轻易的支持草稿存储功能,而无需在业务表里面增加字段。
[![Gem Version](https://badge.fury.io/rb/action-draft.svg)](https://rubygems.org/gems/action-draft) [![Build Status](https://travis-ci.org/rails-engine/action-draft.svg?branch=master)](https://travis-ci.org/rails-engine/action-draft)
## 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
```ruby
gem "action-draft"
```And then execute:
```bash
$ bundle
$ rails action_draft:install:migrations
```## Usage
In your ActiveRecord model:
```rb
# 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:
```erb
<%# app/views/messages/_form.html.erb %>
<%= form_with(model: message) do |form| %>
…
<%= form.label :draft_title %>
<%= form.textarea :draft_title %>
<%= form.label :draft_content %>
<%= form.textarea :draft_content %>
…
<% end %>
```In your controller
```rb
class MessagesController < ApplicationController
def new
@message = Message.new
enddef 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
enddef 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
endprivate
def set_message
@message = Message.find(params[:id])
enddef message_params
params.require(:message).perrmit(:draft_title, :draft_content, :publish)
end
end
```Save draft attributes:
```rb
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.saveirb> message.reload
irb> message.draft_title.to_s
"Draft title"
irb> message.draft_content.to_s
"Draft message content"
```Apply draft content:
```rb
irb> message = Message.new
irb> message.draft_title = "Message title"
irb> message.apply_draftirb> 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](https://opensource.org/licenses/MIT).