Skip to content

Commit

Permalink
add reform
Browse files Browse the repository at this point in the history
  • Loading branch information
tagty committed Feb 25, 2019
1 parent f5e9c39 commit eafed53
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 15 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ gem 'jbuilder', '~> 2.5'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false

gem 'reform'
gem "reform-rails"

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
Expand Down
23 changes: 23 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ GEM
coffee-script-source (1.12.2)
concurrent-ruby (1.1.4)
crass (1.0.4)
declarative (0.0.10)
declarative-builder (0.1.0)
declarative-option (< 0.2.0)
declarative-option (0.1.0)
disposable (0.4.4)
declarative (>= 0.0.9, < 1.0.0)
declarative-builder (< 0.2.0)
declarative-option (< 0.2.0)
representable (>= 2.4.0, <= 3.1.0)
uber (< 0.2.0)
erubi (1.8.0)
execjs (2.7.0)
ffi (1.10.0)
Expand Down Expand Up @@ -140,7 +150,17 @@ GEM
rb-fsevent (0.10.3)
rb-inotify (0.10.0)
ffi (~> 1.0)
reform (2.2.4)
disposable (>= 0.4.1)
representable (>= 2.4.0, < 3.1.0)
reform-rails (0.1.7)
activemodel (>= 3.2)
reform (>= 2.2.0)
regexp_parser (1.3.0)
representable (3.0.4)
declarative (< 0.1.0)
declarative-option (< 0.2.0)
uber (< 0.2.0)
ruby_dep (1.5.0)
rubyzip (1.2.2)
sass (3.7.3)
Expand Down Expand Up @@ -177,6 +197,7 @@ GEM
turbolinks-source (5.2.0)
tzinfo (1.2.5)
thread_safe (~> 0.1)
uber (0.1.0)
uglifier (4.1.20)
execjs (>= 0.3.0, < 3)
web-console (3.7.0)
Expand Down Expand Up @@ -204,6 +225,8 @@ DEPENDENCIES
pg (>= 0.18, < 2.0)
puma (~> 3.11)
rails (~> 5.2.2)
reform
reform-rails
sass-rails (~> 5.0)
selenium-webdriver
spring
Expand Down
24 changes: 13 additions & 11 deletions app/controllers/albums_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ def show

# GET /albums/new
def new
@album = Album.new
@album.build_artist
@album.songs.build
@album = AlbumForm.new(build_album)
end

# GET /albums/1/edit
def edit
@album = AlbumForm.new(@album)
end

# POST /albums
# POST /albums.json
def create
@album = Album.new(album_params)
@album = AlbumForm.new(build_album)

respond_to do |format|
if @album.save
if @album.validate(params[:album])
@album.save
format.html { redirect_to @album, notice: 'Album was successfully created.' }
format.json { render :show, status: :created, location: @album }
else
Expand All @@ -42,8 +42,10 @@ def create
# PATCH/PUT /albums/1
# PATCH/PUT /albums/1.json
def update
@album = AlbumForm.new(@album)
respond_to do |format|
if @album.update(album_params)
if @album.validate(params[:album])
@album.save
format.html { redirect_to @album, notice: 'Album was successfully updated.' }
format.json { render :show, status: :ok, location: @album }
else
Expand All @@ -69,10 +71,10 @@ def set_album
@album = Album.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def album_params
params.require(:album).permit(:title,
artist_attributes: [:name],
songs_attributes: [:title])
def build_album
album = Album.new
album.build_artist
album.songs.build
album
end
end
13 changes: 13 additions & 0 deletions app/forms/album_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class AlbumForm < Reform::Form
property :title
validates :title, presence: true

property :artist do
property :name
validates :name, presence: true
end

collection :songs do
property :title
end
end
3 changes: 0 additions & 3 deletions app/models/album.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
class Album < ApplicationRecord
has_one :artist
has_many :songs

validates :title, presence: true
accepts_nested_attributes_for :artist, :songs
end
1 change: 0 additions & 1 deletion app/models/artist.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
class Artist < ApplicationRecord
validates :name, presence: true
end

0 comments on commit eafed53

Please sign in to comment.