railstar 0.0.9 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+ module Railstar
3
+ module ActiveRecordExt
4
+ module ClassMethods
5
+ def truncation
6
+ return if self.count > 0
7
+ self.truncation!
8
+ end
9
+
10
+ def truncation!
11
+ table_name = self.to_s.underscore.pluralize
12
+ file_name = "#{table_name}.yml"
13
+ file_path = File.join(Rails.root, "resources", "db", file_name)
14
+ raise "#{file_path} file not found." unless File.exist?(file_path)
15
+ self.transaction do
16
+ case self.connection.adapter_name
17
+ when "SQLite"
18
+ self.connection.execute("DELETE FROM `#{self.table_name}`")
19
+ else
20
+ self.connection.execute("TRUNCATE TABLE `#{self.table_name}`")
21
+ end
22
+ YAML.load_file(file_path).each do |key, value|
23
+ self.create value
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ module InstanceMethods
30
+ end
31
+
32
+ def self.included(base)
33
+ base.extend ClassMethods
34
+ base.class_eval do
35
+ include InstanceMethods
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,92 @@
1
+ # -*- coding: utf-8 -*-
2
+ module Railstar
3
+ class SearchBase
4
+ attr_accessor :where, :values
5
+
6
+ def initialize(hash)
7
+ self.where = []
8
+ self.values = {}
9
+ if hash
10
+ hash.each do |k,v|
11
+ send("#{k}=", v)
12
+ end
13
+ end
14
+ end
15
+
16
+ def to_params
17
+ ret = {}
18
+ TARGET_COLUMN.each do |c|
19
+ unless eval(c.to_s).blank?
20
+ ret[c] = eval(c.to_s)
21
+ end
22
+ end
23
+ return ret
24
+ end
25
+
26
+ def to_str_params
27
+ ret = to_params.map do |k,v|
28
+ begin
29
+ "#{k}=#{CGI.escape(v)}"
30
+ rescue
31
+ end
32
+ end
33
+ ret.join("&")
34
+ end
35
+
36
+ def base
37
+ create_conditions #TODO: 毎回条件作成をしないようにしたい
38
+ target_model.where([self.where.join(" AND "), values])
39
+ end
40
+
41
+ private
42
+ def like(method, options={})
43
+ value = eval(method.to_s)
44
+ unless value.blank?
45
+ column = options[:column] || method
46
+ self.where << "#{with_table_name(options[:table_name],column)} like :#{method}"
47
+ self.values[method] = "%#{value}%"
48
+ end
49
+ end
50
+
51
+ def eq(method, options={})
52
+ compare(method, "=", options)
53
+ end
54
+
55
+ def inc(method, options={})
56
+ value = eval(method.to_s)
57
+ value = value.split(",") if value.is_a?(String) && value.include?(",")
58
+ unless value.blank?
59
+ column = options[:column] || method
60
+ self.where << "#{with_table_name(options[:table_name],column)} in (:#{method})"
61
+ self.values[method] = value
62
+ end
63
+ end
64
+
65
+ def compare(method, sign, options={})
66
+ return unless sign =~ /^[<>=]{1,2}$/
67
+ value = eval(method.to_s)
68
+ unless value.blank?
69
+ column = options[:column] || method
70
+ self.where << "#{with_table_name(options[:table_name],column)} #{sign} :#{method}"
71
+ self.values[method] = value
72
+ end
73
+ end
74
+
75
+ def bit(method, options={})
76
+ value = eval(method.to_s)
77
+ unless value.blank?
78
+ column = options[:column] || method
79
+ self.where << "#{with_table_name(options[:table_name],column)} & :#{method} = :#{method}"
80
+ self.values[method] = value
81
+ end
82
+ end
83
+
84
+
85
+ def with_table_name table_name, method
86
+ ret = ""
87
+ ret << "#{table_name || target_model.table_name}." if table_name
88
+ ret << method.to_s
89
+ ret
90
+ end
91
+ end
92
+ end
@@ -1,3 +1,3 @@
1
1
  module Railstar
2
- VERSION = "0.0.9"
2
+ VERSION = "0.0.10"
3
3
  end
data/lib/railstar.rb CHANGED
@@ -1,7 +1,11 @@
1
1
  require 'railstar/engine'
2
2
  require 'railstar/code_holder'
3
+ require 'railstar/search_base'
4
+
3
5
  require 'railstar/helper'
4
6
  ActionView::Base.send(:include, Railstar::Helper)
7
+ require 'railstar/active_record_ext'
8
+ ActiveRecord::Base.send(:include, Railstar::ActiveRecordExt)
5
9
 
6
10
  module Railstar
7
11
  def self.env
@@ -5,7 +5,8 @@
5
5
 
6
6
  class ProjectsController < ApplicationController
7
7
  def index
8
- @projects = Project.order("created_at desc").all
8
+ @search = Search::Project.new(params[:search])
9
+ @projects = @search.base.joins("inner join tasks on tasks.project_id = projects.id").order("created_at desc").group("projects.id").all
9
10
  end
10
11
 
11
12
  def show
@@ -1,83 +1,61 @@
1
+ # -*- coding: utf-8 -*-
2
+ # destroyメソッドに注意。モバイル対応&テキストリンク利用のためイレギュラーなことをしている
3
+ # restに対応した形を徹底したい場合は、削除確認ページへのリンクを以下の様に、formにする
4
+ # <%= form_tag(Task_path(task, :mode => "draft"), :method=>:delete) do %><%= submit_tag "delete", :name => "delete" %><% end %>
5
+
1
6
  class TasksController < ApplicationController
2
- # GET /tasks
3
- # GET /tasks.json
4
7
  def index
5
- @tasks = Task.all
6
-
7
- respond_to do |format|
8
- format.html # index.html.erb
9
- format.json { render json: @tasks }
10
- end
8
+ @search = Search::Task.new(params[:search])
9
+ @search.project_id = params[:project_id]
10
+ @tasks = @search.base.order("created_at desc").all
11
11
  end
12
12
 
13
- # GET /tasks/1
14
- # GET /tasks/1.json
15
13
  def show
16
14
  @task = Task.find(params[:id])
17
-
18
- respond_to do |format|
19
- format.html # show.html.erb
20
- format.json { render json: @task }
21
- end
15
+ render "destroy" if params[:mode] == "draft"
22
16
  end
23
17
 
24
- # GET /tasks/new
25
- # GET /tasks/new.json
26
18
  def new
27
- @task = Task.new
28
-
29
- respond_to do |format|
30
- format.html # new.html.erb
31
- format.json { render json: @task }
32
- end
19
+ @task = Task.new(params[:task])
33
20
  end
34
21
 
35
- # GET /tasks/1/edit
36
22
  def edit
37
23
  @task = Task.find(params[:id])
38
24
  end
39
25
 
40
- # POST /tasks
41
- # POST /tasks.json
42
26
  def create
43
27
  @task = Task.new(params[:task])
44
-
45
- respond_to do |format|
46
- if @task.save
47
- format.html { redirect_to @task, notice: 'Task was successfully created.' }
48
- format.json { render json: @task, status: :created, location: @task }
49
- else
50
- format.html { render action: "new" }
51
- format.json { render json: @task.errors, status: :unprocessable_entity }
52
- end
53
- end
28
+ save
54
29
  end
55
30
 
56
- # PUT /tasks/1
57
- # PUT /tasks/1.json
58
31
  def update
59
32
  @task = Task.find(params[:id])
60
-
61
- respond_to do |format|
62
- if @task.update_attributes(params[:task])
63
- format.html { redirect_to @task, notice: 'Task was successfully updated.' }
64
- format.json { head :no_content }
65
- else
66
- format.html { render action: "edit" }
67
- format.json { render json: @task.errors, status: :unprocessable_entity }
68
- end
69
- end
33
+ @task.attributes = params[:task]
34
+ save
70
35
  end
71
36
 
72
- # DELETE /tasks/1
73
- # DELETE /tasks/1.json
74
37
  def destroy
75
38
  @task = Task.find(params[:id])
76
- @task.destroy
39
+ if params.key?("back")
40
+ redirect_to(task_url)
41
+ elsif params[:mode] != "draft"
42
+ @task.destroy
43
+ redirect_to(task_url, :notice => "deleted success.")
44
+ end
45
+ end
77
46
 
78
- respond_to do |format|
79
- format.html { redirect_to tasks_url }
80
- format.json { head :no_content }
47
+
48
+ private
49
+ def save
50
+ if !params.key?("back") && @task.valid?
51
+ if params[:mode] == "draft"
52
+ render :action => 'confirm'
53
+ else
54
+ @task.save!
55
+ redirect_to task_path(@task), :notice => 'saved success.'
56
+ end
57
+ else
58
+ render (@task.new_record? ? :new : :edit)
81
59
  end
82
60
  end
83
61
  end
@@ -0,0 +1,14 @@
1
+ class Search::Project < Railstar::SearchBase
2
+ TARGET_COLUMN = %w(name like_name)
3
+ attr_accessor *TARGET_COLUMN
4
+
5
+ private
6
+ def create_conditions
7
+ eq(:name)
8
+ like(:like_name, column: :name, table_name: "tasks")
9
+ end
10
+
11
+ def target_model
12
+ ::Project
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ class Search::Task < Railstar::SearchBase
2
+ TARGET_COLUMN = %w(name like_name status project_id from_price to_price)
3
+ attr_accessor *TARGET_COLUMN
4
+
5
+ private
6
+ def create_conditions
7
+ eq(:project_id)
8
+ eq(:name)
9
+ like(:like_name, column: :name)
10
+ inc(:status)
11
+ compare(:from_price, ">=", column: :price)
12
+ compare(:to_price, "<=", column: :price)
13
+ end
14
+
15
+ def target_model
16
+ ::Task
17
+ end
18
+ end
@@ -1 +1,2 @@
1
- <%= link_to "Railstar", railstar.root_path %>
1
+ <%= link_to "Railstar", railstar.root_path %><br />
2
+ <%= link_to "TestProject", projects_path %><br />
@@ -1,10 +1,16 @@
1
1
  <h1>Listing projects</h1>
2
2
 
3
+ <%= form_tag(nil, :method => :get) do %>
4
+ <%= label :search, :name %>:<%= text_field :search, :name %><br />
5
+ <%= label :search, :like_name %>:<%= text_field :search, :like_name %><br />
6
+ <%= submit_tag :search %>
7
+ <% end %>
8
+
3
9
  <table>
4
10
  <thead>
5
11
  <tr>
6
- <th>Name</th>
7
- <th></th>
12
+ <th>Name</th>
13
+ <th></th>
8
14
  <th></th>
9
15
  <th></th>
10
16
  </tr>
@@ -14,9 +20,8 @@
14
20
  <tbody>
15
21
  <% @projects.each do |project| %>
16
22
  <tr>
17
- <th>Name</th>
18
- <td><%= project.name %></td>
19
- <td><%= link_to 'Show', project %></td>
23
+ <td><%= link_to project.name, project_tasks_path(:project_id => project.id) %></td>
24
+ <td><%= link_to 'Show', project %></td>
20
25
  <td><%= link_to 'Edit', edit_project_path(project) %></td>
21
26
  <td><%= link_to 'Delete', project_path(project, :mode => "draft") %></td>
22
27
  </tr>
@@ -0,0 +1,8 @@
1
+ <table class="bordered-table zebra-striped">
2
+
3
+ <tr>
4
+ <th>Name</th>
5
+ <td><%= @task.name %></td>
6
+ </tr>
7
+
8
+ </table>
@@ -1,29 +1,17 @@
1
- <%= form_for(@task) do |f| %>
2
- <% if @task.errors.any? %>
3
- <div id="error_explanation">
4
- <h2><%= pluralize(@task.errors.count, "error") %> prohibited this task from being saved:</h2>
1
+ <%= form_for(@task, :url => save_action(@task, :mode => "draft")) do |f| %>
2
+ <% if @task.errors.any? %>
3
+ <% @task.errors.full_messages.each do |msg| %>
4
+ <p><%= msg %></p>
5
+ <% end %>
6
+ <% end %>
7
+
5
8
 
6
- <ul>
7
- <% @task.errors.full_messages.each do |msg| %>
8
- <li><%= msg %></li>
9
- <% end %>
10
- </ul>
11
- </div>
12
- <% end %>
9
+ <table class="bordered-table zebra-striped">
10
+ <tr>
11
+ <th><%= f.label :name %></th>
12
+ <td><%= f.text_field :name %></td>
13
+ </tr>
14
+ </table>
13
15
 
14
- <div class="field">
15
- <%= f.label :name %><br />
16
- <%= f.text_field :name %>
17
- </div>
18
- <div class="field">
19
- <%= f.label :project_id %><br />
20
- <%= f.number_field :project_id %>
21
- </div>
22
- <div class="field">
23
- <%= f.label :status %><br />
24
- <%= f.number_field :status %>
25
- </div>
26
- <div class="actions">
27
- <%= f.submit %>
28
- </div>
16
+ <%= f.submit :class => "btn primary" %>
29
17
  <% end %>
@@ -0,0 +1,9 @@
1
+ <h1>Confirm task</h1>
2
+
3
+ <%= render :partial => 'detail' %>
4
+
5
+ <%= form_tag({}, :method => request.request_method) do %>
6
+ <%= raw params_to_hidden_tag :task %>
7
+ <input type="submit" value="戻る" name="back" class="btn" />
8
+ <input type="submit" value="保存" class="btn primary" />
9
+ <% end %>
@@ -0,0 +1,12 @@
1
+ <h1>Destroy task</h1>
2
+
3
+ <%= render :partial => 'detail' %>
4
+
5
+ <%= form_tag({}, :method => "delete") do -%>
6
+ <input type="submit" value="Cancel" name="back" class="btn" />
7
+ <input type="submit" value="Destroy" class="btn error" />
8
+ <% end -%>
9
+
10
+ <div>
11
+ <%= link_to 'Back', tasks_path, :class => "btn" %>
12
+ </div>
@@ -1,6 +1,8 @@
1
- <h1>Editing task</h1>
1
+ <h1>Editing project</h1>
2
2
 
3
- <%= render 'form' %>
3
+ <%= render :partial => 'form' %>
4
4
 
5
- <%= link_to 'Show', @task %> |
6
- <%= link_to 'Back', tasks_path %>
5
+ <div>
6
+ <%= link_to 'Show', project_path(@project), :class => "btn" %>
7
+ <%= link_to 'Back', projects_path, :class => "btn" %>
8
+ </div>
@@ -1,27 +1,41 @@
1
1
  <h1>Listing tasks</h1>
2
2
 
3
- <table>
4
- <tr>
5
- <th>Name</th>
6
- <th>Project</th>
7
- <th>Status</th>
8
- <th></th>
9
- <th></th>
10
- <th></th>
11
- </tr>
3
+ <%= form_tag(nil, :method => :get) do %>
4
+ <%= label :search, :name %><%= text_field :search, :name %><br />
5
+ <%= label :search, :like_name %><%= text_field :search, :like_name %><br />
6
+ <% C.status.to_opt.each do |status| %>
7
+ <%= label :search, "status_#{status.first}" %><%= check_box_tag "search[status][]", status.last, !!@search.status ? @search.status.detect{|s| s == status.last} : false %>
8
+ <% end %><br />
9
+ <%= label :search, :price %><%= text_field :search, :from_price %><%= text_field :search, :to_price %><br />
12
10
 
13
- <% @tasks.each do |task| %>
14
- <tr>
15
- <td><%= task.name %></td>
16
- <td><%= task.project_id %></td>
17
- <td><%= task.status %></td>
18
- <td><%= link_to 'Show', task %></td>
19
- <td><%= link_to 'Edit', edit_task_path(task) %></td>
20
- <td><%= link_to 'Destroy', task, confirm: 'Are you sure?', method: :delete %></td>
21
- </tr>
11
+ <%= submit_tag :search %>
22
12
  <% end %>
23
- </table>
24
13
 
25
- <br />
14
+ <table>
15
+ <thead>
16
+ <tr>
17
+ <th>Name</th>
18
+ <th>Status</th>
19
+ <th>Price</th>
20
+ <th></th>
21
+ <th></th>
22
+ <th></th>
23
+ </tr>
24
+ </thead>
25
+
26
+
27
+ <tbody>
28
+ <% @tasks.each do |task| %>
29
+ <tr>
30
+ <td><%= task.name %></td>
31
+ <td><%= C.status[task.status].name %></td>
32
+ <td><%= task.price %></td>
33
+ <td><%= link_to 'Show', project_task_path(:id => task.id) %></td>
34
+ <td><%#= link_to 'Edit', edit_task_path(task) %></td>
35
+ <td><%#= link_to 'Delete', task_path(task, :mode => "draft") %></td>
36
+ </tr>
37
+ <% end %>
38
+ </tbody>
39
+ </table>
26
40
 
27
- <%= link_to 'New Task', new_task_path %>
41
+ <%= link_to 'New Task', new_project_task_path %>
@@ -1,5 +1,7 @@
1
- <h1>New task</h1>
1
+ <h1>New project</h1>
2
2
 
3
- <%= render 'form' %>
3
+ <%= render :partial => 'form' %>
4
4
 
5
- <%= link_to 'Back', tasks_path %>
5
+ <div>
6
+ <%= link_to 'Back', projects_path, :class => "btn" %>
7
+ </div>
@@ -1,20 +1,8 @@
1
- <p id="notice"><%= notice %></p>
1
+ <h1>Showing project</h1>
2
+ <%= render :partial => 'detail' %>
2
3
 
3
- <p>
4
- <b>Name:</b>
5
- <%= @task.name %>
6
- </p>
4
+ <div>
5
+ <%= link_to 'Edit', edit_project_path(@project), :class => "btn" %>
6
+ <%= link_to 'Back', projects_path, :class => "btn" %>
7
+ </div>
7
8
 
8
- <p>
9
- <b>Project:</b>
10
- <%= @task.project_id %>
11
- </p>
12
-
13
- <p>
14
- <b>Status:</b>
15
- <%= @task.status %>
16
- </p>
17
-
18
-
19
- <%= link_to 'Edit', edit_task_path(@task) %> |
20
- <%= link_to 'Back', tasks_path %>
@@ -1,7 +1,8 @@
1
1
  Rails.application.routes.draw do
2
2
  root :to => "general#index"
3
3
 
4
- resources :projects
5
- resources :tasks
4
+ resources :projects do
5
+ resources :tasks
6
+ end
6
7
  mount Railstar::Engine => "/railstar"
7
8
  end
Binary file
@@ -3,7 +3,8 @@ class CreateTasks < ActiveRecord::Migration
3
3
  create_table :tasks do |t|
4
4
  t.string :name
5
5
  t.integer :project_id
6
- t.integer :status
6
+ t.string :status
7
+ t.integer :price
7
8
 
8
9
  t.timestamps
9
10
  end
@@ -22,7 +22,8 @@ ActiveRecord::Schema.define(:version => 20120306035929) do
22
22
  create_table "tasks", :force => true do |t|
23
23
  t.string "name"
24
24
  t.integer "project_id"
25
- t.integer "status"
25
+ t.string "status"
26
+ t.integer "price"
26
27
  t.datetime "created_at", :null => false
27
28
  t.datetime "updated_at", :null => false
28
29
  end
@@ -0,0 +1,4 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ Project.truncation!
4
+ Task.truncation!