From f431437287a9de20c9f9964191a463efa088b649 Mon Sep 17 00:00:00 2001 From: Artem Bubnov Date: Wed, 28 Oct 2015 17:06:11 +0300 Subject: [PATCH 1/4] showbill added --- Gemfile | 14 ++++---------- Gemfile.lock | 3 +++ app/assets/javascripts/showbill.coffee | 7 +++++++ app/controllers/showbill_controller.rb | 17 +++++++++++++++++ app/controllers/user_controller.rb | 1 + app/helpers/showbill_helper.rb | 11 +++++++++++ app/models/showbill.rb | 2 ++ app/views/showbill/_showbill.html.slim | 4 ++++ app/views/showbill/update.js.slim | 5 +++++ app/views/user/index.html.slim | 2 ++ config/locales/en.yml | 1 + config/routes.rb | 2 ++ db/migrate/20151028101608_create_showbills.rb | 9 +++++++++ db/schema.rb | 8 +++++++- db/seeds.rb | 6 +++++- 15 files changed, 80 insertions(+), 12 deletions(-) create mode 100644 app/assets/javascripts/showbill.coffee create mode 100644 app/controllers/showbill_controller.rb create mode 100644 app/helpers/showbill_helper.rb create mode 100644 app/models/showbill.rb create mode 100644 app/views/showbill/_showbill.html.slim create mode 100644 app/views/showbill/update.js.slim create mode 100644 db/migrate/20151028101608_create_showbills.rb diff --git a/Gemfile b/Gemfile index a2cb67a..8cba61f 100644 --- a/Gemfile +++ b/Gemfile @@ -45,15 +45,6 @@ group :development, :test do gem 'mysql2', '~> 0.3.18' end -gem 'slim-rails' -gem 'activeadmin', '~> 1.0.0.pre2' -gem 'devise' -gem 'bullet', :group => 'development' -gem 'jquery-ui-rails' -gem 'exception_notification' -gem 'pundit' -gem 'bootstrap-sass', '~> 3.3.5' - group :test do gem 'cucumber-rails', :require => false # database_cleaner is not required, but highly recommended @@ -71,4 +62,7 @@ gem 'activeadmin', '~> 1.0.0.pre2' gem 'devise' gem 'bullet', :group => 'development' gem 'jquery-ui-rails' -gem 'exception_notification' \ No newline at end of file +gem 'pundit' +gem 'bootstrap-sass', '~> 3.3.5' +gem 'exception_notification' +gem 'rails_autolink' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 3412df3..2ed02b8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -184,6 +184,8 @@ GEM rails_12factor (0.0.3) rails_serve_static_assets rails_stdout_logging + rails_autolink (1.1.6) + rails (> 3.1) rails_serve_static_assets (0.0.4) rails_stdout_logging (0.0.4) railties (4.2.0) @@ -295,6 +297,7 @@ DEPENDENCIES pundit rails (= 4.2.0) rails_12factor + rails_autolink rspec-rails (~> 3.0) sass-rails (~> 5.0) sdoc (~> 0.4.0) diff --git a/app/assets/javascripts/showbill.coffee b/app/assets/javascripts/showbill.coffee new file mode 100644 index 0000000..6eb6482 --- /dev/null +++ b/app/assets/javascripts/showbill.coffee @@ -0,0 +1,7 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ + +$(document).on "ready", -> + $('.showbill').on "keyup", -> + $.ajax({method: "PATCH", url: "showbill/1", data: {showbill: {description: $(this).val()}, id: $(this).attr("id")}, dataType:"script"}) \ No newline at end of file diff --git a/app/controllers/showbill_controller.rb b/app/controllers/showbill_controller.rb new file mode 100644 index 0000000..a26ddf1 --- /dev/null +++ b/app/controllers/showbill_controller.rb @@ -0,0 +1,17 @@ +class ShowbillController < ApplicationController + + def update + showbill = Showbill.where(id: params[:id]).first + + if current_user.director? + @error = t(:error_update_text) unless showbill.update(user_params) + else + @error = t(:you_can_not_edit_it) + end + end + + private + def user_params + params.require(:showbill).permit(:description) + end +end diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 197f1bc..0357764 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -28,5 +28,6 @@ def users_to_show else @users = User.order(:name) end + @showbills = Showbill.order(:description) end end diff --git a/app/helpers/showbill_helper.rb b/app/helpers/showbill_helper.rb new file mode 100644 index 0000000..5662853 --- /dev/null +++ b/app/helpers/showbill_helper.rb @@ -0,0 +1,11 @@ +require 'rails_autolink' + +module ShowbillHelper + def showbill_tag(id, content) + if current_user.present? && current_user.director? + text_area_tag("showbill", content, {class: "showbill width-max-avail", id: id}) + else + content_tag("div", auto_link(content)) + end + end +end diff --git a/app/models/showbill.rb b/app/models/showbill.rb new file mode 100644 index 0000000..f603fb2 --- /dev/null +++ b/app/models/showbill.rb @@ -0,0 +1,2 @@ +class Showbill < ActiveRecord::Base +end diff --git a/app/views/showbill/_showbill.html.slim b/app/views/showbill/_showbill.html.slim new file mode 100644 index 0000000..dbdcf8f --- /dev/null +++ b/app/views/showbill/_showbill.html.slim @@ -0,0 +1,4 @@ +- @showbills.each do |showbill| + .row + .col-md-12 + = showbill_tag(showbill.id, showbill.description) \ No newline at end of file diff --git a/app/views/showbill/update.js.slim b/app/views/showbill/update.js.slim new file mode 100644 index 0000000..aeda320 --- /dev/null +++ b/app/views/showbill/update.js.slim @@ -0,0 +1,5 @@ +- if @error + | $("#error_label").text(" + = @error + | "); + | $("#error_row").show(); \ No newline at end of file diff --git a/app/views/user/index.html.slim b/app/views/user/index.html.slim index c237c59..d5e7ad0 100644 --- a/app/views/user/index.html.slim +++ b/app/views/user/index.html.slim @@ -1,3 +1,5 @@ += render :partial => '/showbill/showbill', :showbills => @showbills + - @users.each do |user| .row id = user.name_without_spaces .col-md-2 class = "name bold" diff --git a/config/locales/en.yml b/config/locales/en.yml index a70cb80..4c9d7e5 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -36,3 +36,4 @@ en: user_details: "User Details" sign_up: "Sign up" or: "or" + you_can_not_edit_it: "You can not edit it!" diff --git a/config/routes.rb b/config/routes.rb index 15e9606..8825ab9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,9 @@ Rails.application.routes.draw do + devise_for :users, :controllers => {:registrations => "user/registrations"} resources :user, only: [:index, :update] + resources :showbill, only: [:index, :update, :show] devise_for :admin_users, ActiveAdmin::Devise.config ActiveAdmin.routes(self) diff --git a/db/migrate/20151028101608_create_showbills.rb b/db/migrate/20151028101608_create_showbills.rb new file mode 100644 index 0000000..1656046 --- /dev/null +++ b/db/migrate/20151028101608_create_showbills.rb @@ -0,0 +1,9 @@ +class CreateShowbills < ActiveRecord::Migration + def change + create_table :showbills do |t| + t.text :description + + t.timestamps null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb index bcc3447..cad227c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20151021063507) do +ActiveRecord::Schema.define(version: 20151028101608) do create_table "active_admin_comments", force: :cascade do |t| t.string "namespace", limit: 255 @@ -46,6 +46,12 @@ add_index "admin_users", ["email"], name: "index_admin_users_on_email", unique: true, using: :btree add_index "admin_users", ["reset_password_token"], name: "index_admin_users_on_reset_password_token", unique: true, using: :btree + create_table "showbills", force: :cascade do |t| + t.text "description", limit: 65535 + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "users", force: :cascade do |t| t.string "email", limit: 255, default: "", null: false t.string "encrypted_password", limit: 255, default: "", null: false diff --git a/db/seeds.rb b/db/seeds.rb index e7aa932..ab4560d 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -32,4 +32,8 @@ user.task = 'Build a car' user.password = '12345678' user.password_confirmation = '12345678' -user.save! \ No newline at end of file +user.save! + +showbill = Showbill.new +showbill.description = 'Some description' +showbill.save! \ No newline at end of file From be13c5de7e697b05e4a3c43ff1d17760ba753775 Mon Sep 17 00:00:00 2001 From: Artem Bubnov Date: Thu, 29 Oct 2015 15:36:49 +0300 Subject: [PATCH 2/4] signed user can edit showbill --- app/assets/javascripts/showbill.coffee | 29 +++++++++++++++++++++++++- app/controllers/showbill_controller.rb | 7 ++++--- app/helpers/showbill_helper.rb | 9 -------- app/views/showbill/_showbill.html.slim | 8 +++++-- app/views/showbill/update.js.slim | 5 ++++- 5 files changed, 42 insertions(+), 16 deletions(-) diff --git a/app/assets/javascripts/showbill.coffee b/app/assets/javascripts/showbill.coffee index 6eb6482..37d1a3c 100644 --- a/app/assets/javascripts/showbill.coffee +++ b/app/assets/javascripts/showbill.coffee @@ -4,4 +4,31 @@ $(document).on "ready", -> $('.showbill').on "keyup", -> - $.ajax({method: "PATCH", url: "showbill/1", data: {showbill: {description: $(this).val()}, id: $(this).attr("id")}, dataType:"script"}) \ No newline at end of file + $.ajax({method: "PATCH", url: "showbill/" + $(this).attr("id"), data: make_data($(this)) , dataType:"script"}) + + # converts div.html to textarea.text + $('.showbill-div').on "click", -> + if $('textarea').is('.showbill') + text = $('.showbill-div').html().replace( /
/g,"\n") + $('.showbill').text(disable_links(clean_text(text))) + $('.showbill').show().focus() + $('.showbill-div').hide() + + $('.showbill').on "blur", -> + $('.showbill').hide() + $('.showbill-div').show() + +make_data = (item) -> + return {} = + showbill:{ + #we'll need description in one line. Replacing \n by
to don't lose them. + description: item.val().replace(/\n/g, "
")} + id: $(item).attr("id") + +#extracts link_text from link_tag by deleting and +disable_links = (text) -> + text.replace(//g, "").replace(/<\/a>/g,"") + +clean_text = (text) -> + text.replace(/^\n*\s*\n*\s*/,"").replace(/\s*$/,"") + diff --git a/app/controllers/showbill_controller.rb b/app/controllers/showbill_controller.rb index a26ddf1..f43bb4d 100644 --- a/app/controllers/showbill_controller.rb +++ b/app/controllers/showbill_controller.rb @@ -1,13 +1,14 @@ class ShowbillController < ApplicationController def update - showbill = Showbill.where(id: params[:id]).first + @showbill = Showbill.where(id: params[:id]).first - if current_user.director? - @error = t(:error_update_text) unless showbill.update(user_params) + if current_user.present? + @error = t(:error_update_text) unless @showbill.update(user_params) else @error = t(:you_can_not_edit_it) end + end private diff --git a/app/helpers/showbill_helper.rb b/app/helpers/showbill_helper.rb index 5662853..10627c7 100644 --- a/app/helpers/showbill_helper.rb +++ b/app/helpers/showbill_helper.rb @@ -1,11 +1,2 @@ -require 'rails_autolink' - module ShowbillHelper - def showbill_tag(id, content) - if current_user.present? && current_user.director? - text_area_tag("showbill", content, {class: "showbill width-max-avail", id: id}) - else - content_tag("div", auto_link(content)) - end - end end diff --git a/app/views/showbill/_showbill.html.slim b/app/views/showbill/_showbill.html.slim index dbdcf8f..e309b4e 100644 --- a/app/views/showbill/_showbill.html.slim +++ b/app/views/showbill/_showbill.html.slim @@ -1,4 +1,8 @@ - @showbills.each do |showbill| .row - .col-md-12 - = showbill_tag(showbill.id, showbill.description) \ No newline at end of file + .col-md-12 class = "showbill-div" + = auto_link(showbill.description) + .row + - if current_user.present? + .col-md-12 + = text_area_tag("showbill", showbill.description, {class: "showbill width-max-avail", id: showbill.id, hidden: "hidden"}) \ No newline at end of file diff --git a/app/views/showbill/update.js.slim b/app/views/showbill/update.js.slim index aeda320..1db7d87 100644 --- a/app/views/showbill/update.js.slim +++ b/app/views/showbill/update.js.slim @@ -2,4 +2,7 @@ | $("#error_label").text(" = @error | "); - | $("#error_row").show(); \ No newline at end of file + | $("#error_row").show(); +- else + | $('.showbill-div').text(""); + | $('.showbill-div').append('#{auto_link(@showbill.description)}'); \ No newline at end of file From a443402fd5c892f71bd211b07104e0cb10f4e969 Mon Sep 17 00:00:00 2001 From: Artem Bubnov Date: Fri, 30 Oct 2015 13:46:44 +0300 Subject: [PATCH 3/4] routes fixed --- config/routes.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index 8825ab9..5163e11 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,7 +3,7 @@ devise_for :users, :controllers => {:registrations => "user/registrations"} resources :user, only: [:index, :update] - resources :showbill, only: [:index, :update, :show] + resources :showbill, only: [:update] devise_for :admin_users, ActiveAdmin::Devise.config ActiveAdmin.routes(self) From 68c16d48a7f1487e7bed929907d11aba0e631cc1 Mon Sep 17 00:00:00 2001 From: Artem Bubnov Date: Fri, 30 Oct 2015 17:43:54 +0300 Subject: [PATCH 4/4] singleton created, model deleted --- app/assets/javascripts/showbill.coffee | 5 ++--- app/controllers/showbill_controller.rb | 7 +------ app/controllers/user_controller.rb | 2 +- app/models/showbill.rb | 2 -- app/models/showbill_singleton.rb | 21 +++++++++++++++++++ app/views/showbill/_showbill.html.slim | 15 +++++++------ app/views/showbill/update.js.slim | 3 ++- app/views/user/update.js.slim | 2 ++ db/migrate/20151028101608_create_showbills.rb | 9 -------- db/schema.rb | 8 +------ db/seeds.rb | 6 +----- 11 files changed, 38 insertions(+), 42 deletions(-) delete mode 100644 app/models/showbill.rb create mode 100644 app/models/showbill_singleton.rb delete mode 100644 db/migrate/20151028101608_create_showbills.rb diff --git a/app/assets/javascripts/showbill.coffee b/app/assets/javascripts/showbill.coffee index 37d1a3c..ddaf463 100644 --- a/app/assets/javascripts/showbill.coffee +++ b/app/assets/javascripts/showbill.coffee @@ -20,9 +20,8 @@ $(document).on "ready", -> make_data = (item) -> return {} = - showbill:{ - #we'll need description in one line. Replacing \n by
to don't lose them. - description: item.val().replace(/\n/g, "
")} + #we'll need description in one line. Replacing \n by
to don't lose them. + description: item.val().replace(/\n/g, "
") id: $(item).attr("id") #extracts link_text from link_tag by deleting and diff --git a/app/controllers/showbill_controller.rb b/app/controllers/showbill_controller.rb index f43bb4d..19bd141 100644 --- a/app/controllers/showbill_controller.rb +++ b/app/controllers/showbill_controller.rb @@ -1,18 +1,13 @@ class ShowbillController < ApplicationController def update - @showbill = Showbill.where(id: params[:id]).first if current_user.present? - @error = t(:error_update_text) unless @showbill.update(user_params) + @error = t(:error_update_text) unless @showbill = Showbill_singleton.instance.update(params[:description]) else @error = t(:you_can_not_edit_it) end end - private - def user_params - params.require(:showbill).permit(:description) - end end diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 0357764..907ecb4 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -28,6 +28,6 @@ def users_to_show else @users = User.order(:name) end - @showbills = Showbill.order(:description) + @showbill = Showbill_singleton.instance.text end end diff --git a/app/models/showbill.rb b/app/models/showbill.rb deleted file mode 100644 index f603fb2..0000000 --- a/app/models/showbill.rb +++ /dev/null @@ -1,2 +0,0 @@ -class Showbill < ActiveRecord::Base -end diff --git a/app/models/showbill_singleton.rb b/app/models/showbill_singleton.rb new file mode 100644 index 0000000..b310ae4 --- /dev/null +++ b/app/models/showbill_singleton.rb @@ -0,0 +1,21 @@ +require 'singleton' + +class Showbill_singleton + include Singleton + + attr_accessor :text + + def initialize + @text = "Some default description" + end + + def update(text) + text.present? ? @text = text : false + end + + def text + @text.present? ? @text : initialize + end + + +end \ No newline at end of file diff --git a/app/views/showbill/_showbill.html.slim b/app/views/showbill/_showbill.html.slim index e309b4e..1f9e8d2 100644 --- a/app/views/showbill/_showbill.html.slim +++ b/app/views/showbill/_showbill.html.slim @@ -1,8 +1,7 @@ -- @showbills.each do |showbill| - .row - .col-md-12 class = "showbill-div" - = auto_link(showbill.description) - .row - - if current_user.present? - .col-md-12 - = text_area_tag("showbill", showbill.description, {class: "showbill width-max-avail", id: showbill.id, hidden: "hidden"}) \ No newline at end of file +.row + .col-md-12 class = "showbill-div" + = auto_link(@showbill) +.row + - if current_user.present? + .col-md-12 + = text_area_tag("showbill", @showbill, {class: "showbill width-max-avail", hidden: "hidden"}) \ No newline at end of file diff --git a/app/views/showbill/update.js.slim b/app/views/showbill/update.js.slim index 1db7d87..80b5ea5 100644 --- a/app/views/showbill/update.js.slim +++ b/app/views/showbill/update.js.slim @@ -4,5 +4,6 @@ | "); | $("#error_row").show(); - else + | $("#error_row").hide(); | $('.showbill-div').text(""); - | $('.showbill-div').append('#{auto_link(@showbill.description)}'); \ No newline at end of file + | $('.showbill-div').append('#{auto_link(@showbill)}'); \ No newline at end of file diff --git a/app/views/user/update.js.slim b/app/views/user/update.js.slim index 9843362..ccc2f8d 100644 --- a/app/views/user/update.js.slim +++ b/app/views/user/update.js.slim @@ -4,3 +4,5 @@ = @error | "); | $("#error_row").show(); +- else + | $("#error_row").hide(); \ No newline at end of file diff --git a/db/migrate/20151028101608_create_showbills.rb b/db/migrate/20151028101608_create_showbills.rb deleted file mode 100644 index 1656046..0000000 --- a/db/migrate/20151028101608_create_showbills.rb +++ /dev/null @@ -1,9 +0,0 @@ -class CreateShowbills < ActiveRecord::Migration - def change - create_table :showbills do |t| - t.text :description - - t.timestamps null: false - end - end -end diff --git a/db/schema.rb b/db/schema.rb index cad227c..bcc3447 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20151028101608) do +ActiveRecord::Schema.define(version: 20151021063507) do create_table "active_admin_comments", force: :cascade do |t| t.string "namespace", limit: 255 @@ -46,12 +46,6 @@ add_index "admin_users", ["email"], name: "index_admin_users_on_email", unique: true, using: :btree add_index "admin_users", ["reset_password_token"], name: "index_admin_users_on_reset_password_token", unique: true, using: :btree - create_table "showbills", force: :cascade do |t| - t.text "description", limit: 65535 - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - end - create_table "users", force: :cascade do |t| t.string "email", limit: 255, default: "", null: false t.string "encrypted_password", limit: 255, default: "", null: false diff --git a/db/seeds.rb b/db/seeds.rb index ab4560d..e7aa932 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -32,8 +32,4 @@ user.task = 'Build a car' user.password = '12345678' user.password_confirmation = '12345678' -user.save! - -showbill = Showbill.new -showbill.description = 'Some description' -showbill.save! \ No newline at end of file +user.save! \ No newline at end of file