diff --git a/app/controllers/domains_controller.rb b/app/controllers/domains_controller.rb new file mode 100644 index 0000000..810792d --- /dev/null +++ b/app/controllers/domains_controller.rb @@ -0,0 +1,56 @@ +class DomainsController < ApplicationController + before_action :set_domain, only: [:show, :edit, :update, :destroy] + + # GET /domains + def index + @domains = Domain.all + end + + # GET /domains/1 + def show + end + + # GET /domains/new + def new + @domain = Domain.new + end + + # GET /domains/1/edit + def edit + end + + # POST /domains + def create + @domain = Domain.new(domain_params) + + if @domain.save + redirect_to @domain, notice: "#{@domain.name} was successfully created." + else + render :new + end + end + + # PATCH/PUT /domains/1 + def update + if @domain.update(domain_params) + redirect_to @domain, notice: "#{@domain.name} was successfully updated." + else + render :edit + end + end + + # DELETE /domains/1 + def destroy + @domain.destroy + redirect_to domains_url, notice: "#{@domain.name} was successfully destroyed." + end + + private + def set_domain + @domain = Domain.find(params[:id]) + end + + def domain_params + params.require(:domain).permit(:name, :type) + end +end diff --git a/app/controllers/records_controller.rb b/app/controllers/records_controller.rb new file mode 100644 index 0000000..7ce17fa --- /dev/null +++ b/app/controllers/records_controller.rb @@ -0,0 +1,56 @@ +class RecordsController < ApplicationController + before_action :set_domain + before_action :set_record, only: [:edit, :update, :destroy] + + # GET /records/new + def new + @record = @domain.records.build + end + + # GET /records/1/edit + def edit + end + + # POST /records + def create + @record = @domain.records.new(new_record_params) + + if @record.save + redirect_to @domain, notice: 'Record was successfully created.' + else + render :new + end + end + + # PATCH/PUT /records/1 + def update + if @record.update(edit_record_params) + redirect_to @domain, notice: 'Record was successfully updated.' + else + render :edit + end + end + + # DELETE /records/1 + def destroy + @record.destroy + redirect_to @domain, notice: 'Record was successfully destroyed.' + end + + private + def set_record + @record = @domain.records.find(params[:id]) + end + + def set_domain + @domain = Domain.find(params[:domain_id]) + end + + def edit_record_params + params.require(:record).permit(:name, :content) + end + + def new_record_params + params.require(:record).permit(:name, :content, :type) + end +end diff --git a/app/views/domains/_form.html.erb b/app/views/domains/_form.html.erb new file mode 100644 index 0000000..3060adf --- /dev/null +++ b/app/views/domains/_form.html.erb @@ -0,0 +1,5 @@ +<%= bootstrap_form_for(@domain, layout: :horizontal, label_col: 'col-sm-2', control_col: 'col-sm-4') do |f| %> + <%= f.text_field :name %> + <%= f.select :type, Domain.domain_types %> + <%= f.submit 'Save', class: 'btn btn-primary col-sm-offset-2' %> +<% end %> diff --git a/app/views/domains/edit.html.erb b/app/views/domains/edit.html.erb new file mode 100644 index 0000000..6690440 --- /dev/null +++ b/app/views/domains/edit.html.erb @@ -0,0 +1,3 @@ +

<%= @domain.name %>

+ +<%= render 'form' %> diff --git a/app/views/domains/index.html.erb b/app/views/domains/index.html.erb new file mode 100644 index 0000000..758d05f --- /dev/null +++ b/app/views/domains/index.html.erb @@ -0,0 +1,22 @@ + + + + + + + + + + <% @domains.each do |domain| %> + + + + + + <% end %> + +
DomainControls
<%= link_to domain.name, domain %><%= link_to 'Edit', edit_domain_path(domain) %><%= link_to 'Destroy', domain, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +

+ <%= link_to 'New Domain »'.html_safe, new_domain_path, class: 'btn btn-lg btn-primary' %> +

diff --git a/app/views/domains/new.html.erb b/app/views/domains/new.html.erb new file mode 100644 index 0000000..0bd162c --- /dev/null +++ b/app/views/domains/new.html.erb @@ -0,0 +1,3 @@ +

Register domain

+ +<%= render 'form' %> diff --git a/app/views/domains/show.html.erb b/app/views/domains/show.html.erb new file mode 100644 index 0000000..b1cef23 --- /dev/null +++ b/app/views/domains/show.html.erb @@ -0,0 +1,23 @@ + + + + + + + + + + <% @domain.records.each do |record| %> + + + + + + + + + <% end %> + +
RecordsControls
<%= record.name %>IN<%= record.type %><%= record.content %><%= link_to 'Edit', edit_domain_record_path(@domain, record) %><%= link_to 'Remove', [@domain, record], method: :delete, data: { confirm: 'Are you sure?' } %>
+ +

<%= link_to 'New Record', new_domain_record_path(@domain) %>

diff --git a/app/views/records/_form.html.erb b/app/views/records/_form.html.erb new file mode 100644 index 0000000..75cf980 --- /dev/null +++ b/app/views/records/_form.html.erb @@ -0,0 +1,13 @@ +<%= bootstrap_form_for([@domain, @record], layout: :horizontal, label_col: 'col-sm-2', control_col: 'col-sm-4') do |f| %> + + <%= f.text_field :name, value: @record.short, label: 'Record', append: ".#{@domain.name}" %> + <% if @record.persisted? %> + <%= f.static_control :type %> + <% else %> + <%= f.select :type, Record.record_types %> + <% end %> + <%= f.text_field :content %> + + <%= f.submit 'Save', class: 'btn btn-primary col-sm-offset-2' %> + +<% end %> diff --git a/app/views/records/edit.html.erb b/app/views/records/edit.html.erb new file mode 100644 index 0000000..fe2105a --- /dev/null +++ b/app/views/records/edit.html.erb @@ -0,0 +1,3 @@ +

Editing record

+ +<%= render 'form' %> diff --git a/app/views/records/new.html.erb b/app/views/records/new.html.erb new file mode 100644 index 0000000..4b9d19b --- /dev/null +++ b/app/views/records/new.html.erb @@ -0,0 +1,4 @@ +

New record

+ +<%= render 'form' %> + diff --git a/config/routes.rb b/config/routes.rb index 1daf9a4..dfdfd7e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,2 +1,7 @@ Rails.application.routes.draw do + get '/', to: redirect('/domains') + + resources :domains do + resources :records, except: [:index, :show] + end end