裡面的教學之一,簡單但是在各種課程都常見的todo list
新手好朋友
https://telsaiori.github.io/udemy_todo_list/index.html
- 比較重要的大概就是
- 學到用stopPropagation
- 用which來判斷是按了什麼鍵
- on(“click”) / click()的差別
Written with StackEdit.
Written with StackEdit.
$('ul').on('click',"li", function() {
});
$('ul').click(function() {
});
簡單說就是on可以用在未來才會出現的元素,click只會對目前已經存在的東西有作用。 $("ul").append("<li><span> X </span>" + todoText + "</li>");
如果本來是用$(“li”).click來設定一下動作的話,新增加的li是不會被套用的Written with StackEdit.
class Conversation < ActiveRecord::Base
belongs_to :sender, :foreign_key => :sender_id, class_name: 'User'
belongs_to :recipient, :foreign_key => :recipient_id, class_name: 'User'
has_many :messages, dependent: :destroy
validates_uniqueness_of :sender_id, :scope => :recipient_id
scope :between, -> (sender_id,recipient_id) do
where("(conversations.sender_id = ? AND conversations.recipient_id =?) OR (conversations.sender_id = ? AND conversations.recipient_id =?)", sender_id,recipient_id, recipient_id, sender_id)
end
end
上面建立了一個between scope,是為了之後方便確認某兩位使用者之間有沒有建立過對話 class Message < ActiveRecord::Base
belongs_to :conversation
belongs_to :user
validates_presence_of :body, :conversation_id, :user_id
def message_time
created_at.strftime("%m/%d/%y at %l:%M %p")
end
end
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :messages
has_many :conversations
end
<div class=”ui segment”>
<h3>Mailbox</h3>
<div class=”ui list”>
<div class=”item”>
<% @conversations.each do |conversation| %>
<% if conversation.sender_id == current_user.id || conversation.receiver_id == current_user.id %>
<% if conversation.sender_id == current_user.id %>
<% recipient = User.find(conversation.receiver_id) %>
<% else %>
<% recipient = User.find(conversation.sender_id) %>
<% end %>
<%= link_to recipient.first_name, conversation_messages_path(conversation)%>
<% end %>
<% end %>
</div>
</div>
</div>
<div class=”ui segment”>
<h3>All Users</h3>
<div class=”ui list”>
<% @users.each do |user| %>
<% if user.id != current_user.id %>
<div class=”item”>
<%= user.first_name %> <%= link_to ‘Message me!’, conversations_path(sender_id: current_user.id, receiver_id: user.id), method: ‘post’%>
</div>
<% end %>
<% end %>
</div>
</div>
mailbox的首頁,controller裡面已經定義好@users和@conversations會撈出所有的使用者和目前所有的對話,然後view在顯示出目前使用者相關的對話,以及可以send訊息的link,點下會連到conversation的create action def create
if Conversation.between(params[:sender_id],params[:recipient_id]).present?
@conversation = Conversation.between(params[:sender_id], params[:recipient_id]).first
else
@conversation = Conversation.create!(conversation_params)
end
redirect_to conversation_messages_path(@conversation)
end
會先判斷這兩個使用者之間有沒有已存在的對話,有則把舊的撈出來,沒有就create,之後的動作會交給message controller def new
@message = @conversation.messages.new
end
def create
@message = @conversation.messages.new(message_params)
if @message.save
redirect_to conversation_messages_path(@conversation)
end
end
private
def message_params
params.require(:message).permit(:body, :user_id)
end
Written with StackEdit.
@string = "abcde"
@l = @string.length
@string_reversed = ""
i = @l-1
while i >=0 do
@string_reversed << @string[i]
i = i-1
end
return @string_reversed
然後被說很不ruby def reverse_string(s)
new_string = ""
s.length.times do |i|
index = (i+1)
new_string << s[-index]
end
new_string
end
這樣有比較ruby一點了嗎orzWritten with StackEdit.
def bubble_sort(list)
list.each_with_index do |no|
(list.length - 1).times do |e|
if list[e] > list[e + 1]
list[e],list[e+1] = list[e+1],list[e]
end
end
end
end
def insertion_sort(list)
list.each_with_index do |no, index|
(0..index).each do |i|
if list[index] < list[i]
list[index], list[i] = list[i], list[index]
end
end
end
end
def select_sort(list)
(list.length - 1).times do |i|
p "#{i} times fo #{list} and the mis is #{list[i..-1].min}"
min_index = list.rindex list[i..-1].min
if list[i] > list[i..-1].min
list[i], list[min_index] = list[i..-1].min,list[i]
end
end
list
end
Written with StackEdit.
因為原來老師的架構和我自己想要的不一樣、所以做了以下修改
因為自動寄sms/email的服務基本上都需要付費才方便使用,所以我移除了那些功能
改成需要手動執行rake來新增AuditLog
desc "Check if there are any overtime work didn't report"
task check_overtime: :environment do
employees = User.where(type: nil)
week_start = Date.today.beginning_of_week
week_end = Date.today.end_of_week
no_overtime_week = []
employees.each do |employee|
no_overtime_week = employee.posts.where(date: @week_start..@week_end)
unless no_overtime_week.any?
AuditLog.create!(user_id: employee.id, status: 0, start_date: week_start, end_date: week_end)
end
end
end
防止同一員工同一個禮拜產生兩次auditlog
validates :start_date, uniqueness: { scope: :user_id }
員工新增顯示沒有加班的weeks,但目前還沒有打算提供修改辦法
新增只要auditlog被新增之後,就沒辦法申請那一個禮拜的加班的限制
def check_audit_log
user = User.find(self.user_id)
my_audit_logs = user.audit_logs.where.not(end_date: nil)
date = self.date
duplicate = ""
my_audit_logs.each do |log|
if (log.start_date..log.end_date).include? date
errors[:overtime] << "duplicate"
return
end
end
Written with StackEdit.
<%= calendar current_date do |date| %>
<% date_key = date.strftime %>
<% if posts_by_date[date_key] %>
<%= link_to date.day, posts_path(posts_by_date: date_key) %>
<% else %>
<%= date.day%>
<% end %>
<% end %>
code:def calendar(date = Date.today, &block)
Calendar.new(self, date, block).table
end
class Calendar < Struct.new(:view, :date, :callback)
Calendar.new(self, date, block).table
end
這裡使用Struct.new(:view, :date, :callback)和設定attr_accessor :view, :date, :callback效果是一樣的 class Calendar < Struct.new(:view, :date, :callback)
HEADER = %w[ 日 一 二 三 四 五 六]
START_DAY = :sunday
delegate :content_tag, to: :view
def table
content_tag :table, class: "calendar" do
year_header + header + week_rows
end
end
下面生成table的過程就比較單純 def header
content_tag :tr do
HEADER.map { |day| content_tag :th, day}.join.html_safe
end
end
生成一個星期的row,先用一開始代進來的date算出這星期的第一天和最後一天,然後和上面一樣轉成htmldef week_rows
weeks.map do |week|
content_tag :tr do
week.map { |day| day_cell(day) }.join.html_safe
end
end.join.html_safe
end
def weeks
first = date.beginning_of_month.beginning_of_week(START_DAY)
last = date.end_of_month.end_of_week(START_DAY)
(first..last).to_a.in_groups_of(7)
end
中間會先把日期丟給day_cell處理 <%= calendar current_date do |date| %>
<% date_key = date.strftime %>
<% if posts_by_date[date_key] %>
<%= link_to date.day, posts_path(posts_by_date: date_key) %>
<% else %>
<%= date.day%>
<% end %>
<% end %>
def day_cell(day)
content_tag :td, view.capture(day, &callback), class: day_classes(day)
end
def day_classes(day)
classes = []
classes << "today" if day == Date.today
classes << "notmonth" if day.month != date.month
classes.empty? ? nil : classes.join(" ")
end
Written with StackEdit.